如何在运行时指定 SQL 查询输入。
前任;
insert into table1 value('b','a');
在运行时键入
a= inn
b= out
cn = new SqlConnection("YourConnectionString");
string sql = "INSERT INTO table1(columnName1, ColumnName2) VALUES (@columnName1, @ColumnName2)";
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.AddWithValue("@columnName1", textbox.text);
cmd.Parameters.AddWithValue("@columnName2", textbox2.text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cn.Close();
希望这可以帮助.....
编辑 - 对于存储过程,您可以这样做:
USE [DatabaseName]
GO
/****** Object: StoredProcedure [dbo].[DatabaseName] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[storedProcName]
@param1 varchar(50)
,@param2 varchar(50)
AS
BEGIN
INSERT INTO [dbo].[tableName]
([ComunName1]
,[ComunName2]
VALUES
(
@param1
,@param2
)
END
在您的代码中,您可以按如下方式调用存储过程:
c#:
cn = new SqlConnection("YourConnectionString");
sql = "yourStoredProcName";
command = new SqlCommand(sql, cn);
command.CommandType = CommandType.StoredProcedure;
command.Parameter.Add(new SqlParameter("@param1", SqlDbType.VarChar)).Value = textbox1.text;
command.Parameter.Add(new SqlParameter("@param2", SqlDbType.VarChar)).Value = textbox2.text;
connection.Open();
command.ExecuteNonQuery();
connection.Close();
希望这有助于存储过程.....
ide=input("enter the id")
prod_name=input("enter the name")
company_name=input("enter the company")
insert into carz values('"+ide+"','"+prod_name+"','"+company_name+"')