2011-05-12 65 views
0

我有一個代碼將數據從asp.net(c#代碼)移動到sql。在下面的代碼中,我直接插入了值(即)xxx和yyy.This也起作用。現在我想插入在文本框中輸入的值。代碼要做什麼?幫助我的朋友。從asp.net檢索文本框數據到sql

protected void Button2_Click(object sender, EventArgs e) 
{ 
    string comm="insert into login values('xxx','yyy')"; 
    con = new SqlConnection(); 
    con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString(); 
    cmd = new SqlCommand(comm, con); 
    cmd.Connection.Open(); 
    con.Open(); 
    int i = cmd.ExecuteNonQuery(); 
    cmd.Connection.Close(); 
} 

回答

2
protected void Button2_Click(object sender, EventArgs e) 
{ 
    string comm="insert into login values(@val1,@val2)";   
    con = new SqlConnection();   
    con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString();   
    cmd = new SqlCommand(comm, con);   
    cmd.Connection.Open(); 

    cmd.Parameters.Add("@val1",TextBox1.Text); //Replace TextBox1.Text with your first textbox 
    cmd.Parameters.Add("@val2",TextBox2.Text); //Replace TextBox2.Text with your second textbox   

    con.Open();   
    int i = cmd.ExecuteNonQuery();   
    cmd.Connection.Close();  
} 
+0

兩次與'@ val1'參數? –

+0

我的壞,謝謝,做了改變 –

+0

它的工作原理,感謝朋友。 – anvisha