我的参数化插入在asp.net中不起作用

时间:2015-07-24 23:24:59

标签: c# sql asp.net

参数化数据插入的代码不起作用。我收到错误:

'System.Data.SqlClient.SqlCommand' does not contain a definition for 'Parameter' and no extension method 'Parameter' accepting a first argument of type 'System.Data.SqlClient.SqlCommand' could be found (are you missing a using directive or an assembly reference?)

我添加了using System.Data.SqlClient.SqlCommand,但没有任何区别。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;    
using System.Data.SqlClient;
using System.Configuration;    

public partial class add_data_ch1 : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\qwhizz_ch1_Database.mdf;Integrated Security=True;User Instance=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();
    }
    protected void btn_Click(object sender, EventArgs e)
    {           
        SqlCommand cmd = new SqlCommand(@"insert into data_Table values (@fname, @lname, con);
        cmd.Parameter.AddWithValue("@fname", fnameTextBox.Text);
        cmd.Parameter.AddWithValue("@lname", lnameTextBox.Text);            
        cmd.ExecuteNonQuery();
        con.Close();              
    }        
}

如果有人能告诉我代码错误在哪里或缺少什么,我们将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

应该是参数

java.library.path

答案 1 :(得分:0)

SqlCommand cmd = new SqlCommand(@"insert into data_Table values (@fname, @lname, con);

我相信你只是缺少一个结束括号和引号。

SqlCommand cmd = new SqlCommand(@"insert into data_Table values (@fname, @lname)", con);