此代码成功地在SQL数据库中插入一个新值,但仅当我插入常量值时。
我需要帮助,它在下面的代码中说**(?)**
,我想在代码中插入新值而不指定常量。 我的意思是,我希望能够在输出窗口中键入任何随机值,并将其插入到SQL数据库中。使用c插入sql#
private void InsertInfo()
{
String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";
SqlConnection con = new SqlConnection(strConnection);
string connetionString = null;
SqlConnection connection ;
SqlDataAdapter adapter = new SqlDataAdapter();
connetionString = @"Data Source=HP\SQLEXPRESS;database=MK;Integrated Security=true";
connection = new SqlConnection(connetionString);
string sql = "insert into record (name,marks) **values(?))";**
try
{
connection.Open();
adapter.InsertCommand = new SqlCommand(sql, connection);
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show ("Row inserted !! ");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void insert_Click(object sender, EventArgs e)
{
InsertInfo();
}
使用参数: http://stackoverflow.com/questions/10718822/ado-net-how-to-add-parameter-if-i -use-connection-class-in-a-separate-file – 2013-02-25 08:21:44
同意@AlexeyRaga。您的代码容易受到SQL注入的攻击。 – SchmitzIT 2013-02-25 08:26:52
@SchmitzIT问题**中显示的代码不容易受到SQL注入的影响;它不显示任何数据值被连接到TSQL。它显示了一些不完整的SQL工作,但这很好 - 我们可以帮助解决这个问题。请不要危言耸听。 – 2013-02-25 08:28:00