SqlConnection conn = new SqlConnection(connstring);//里面要连接字符串
string sql = string.Format("insert into table1(name,address) values('{0}','{1}')",name,address);
SqlCommand command = new SqlCommand(sql);
command.Connection = conn;
conn.Open();
command.ExecuteNonQuery();
conn.Close();

解决方案 »

  1.   

    SqlConnection Conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;" +
                                                "Initial Catalog=你的数据库名");
    Conn.Open();string insertStr = "INSERT INTO 你的数据表(name,address) Values(this.name.Text.ToString(), this.address.Text.ToString())";SqlCommand insertCMD = new SqlCommand(insertStr,Conn);
    insertCMD.ExecuteNonQuery();
      

  2.   

    假设为文本框控件为textBox类的实例textBox1与textBox2,且有一buttion按钮,有一数据库data1,表table1,表中有两个字段,也就是你上面提到的.
    假设引用与命名空间都已确定,自动生成
    private string conn ="server=.;uid=sa;pwd=;database=data1";
    private string str="select * from table1";
    SqlConnection SqlConn = new SqlConnection(conn);
    SqlDataAdapter Sqlad = ndw SqlDataAdapter(str,conn);
    DataSet DS = new  DataSet;
    Sqlad.fill(DS,"table1");
    string text1 = textBox1.text,text2 = textBox2.text;
    string sqltext="insert table (name,address) values ('"+text1+"','"+text2+"')";
    SqlConn.open();
    SqlCommand Sqlcmd = ndw SqlCommand(sqltext,SqlConn);
    Sqlcmd.ExecuteNonQuery();
    SqlConn.Close();