如何在运行时指定SQL查询输入。
实施例;
insert into table1 value('b','a');
要在运行时键入类型
a= inn
b= out
答案 0 :(得分:2)
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();
希望这有助于存储过程.....
答案 1 :(得分:1)
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+"')