SqlDataAdapter AddUserDA=new SqlDataAdapter()
AddUserDA.InsertCommand = new SqlCommand("INSERT INTO userinfo (nickname,loginname,password,sex,age,mobil,email,OICQ,registdate,lastdate) " +
"VALUES (@nickname, @loginname,@password,@sex,@age,@mobil,@email,@oicq,@registdate,@lastdate)",conn);
AddUserDA.InsertCommand.Parameters.Clear();
AddUserDA.InsertCommand.Parameters.Add("@nickname",SqlDbType.VarChar,50);
AddUserDA.InsertCommand.Parameters.Add("@loginname",SqlDbType.VarChar,50);
AddUserDA.InsertCommand.Parameters.Add("@password",SqlDbType.Char,10);
AddUserDA.InsertCommand.Parameters.Add("@sex",SqlDbType.VarChar,2);
AddUserDA.InsertCommand.Parameters.Add("@age",SqlDbType.Int,4);
AddUserDA.InsertCommand.Parameters.Add("@mobil",SqlDbType.Char ,20);
AddUserDA.InsertCommand.Parameters.Add("@email",SqlDbType.VarChar ,100);
AddUserDA.InsertCommand.Parameters.Add("@oicq",SqlDbType.Char,20);
AddUserDA.InsertCommand.Parameters.Add("@registdate",SqlDbType.DateTime,8);
AddUserDA.InsertCommand.Parameters.Add("@lastdate",SqlDbType.DateTime,8); if (ds.Tables[0].Rows.Count >0)
{
for (int i = 0; i< ds.Tables[0].Columns.Count ; i++) 
AddUserDA.InsertCommand.Parameters[i].Value=ds.Tables[0].Rows[0].ItemArray.GetValue(i); } AddUserDA.InsertCommand.Connection.Open();
AddUserDA.InsertCommand.ExecuteNonQuery();
AddUserDA.InsertCommand.Connection.Close();
你好,这是我自己写的一段程序。程序中的DS是由其它地方传过来的。这个程序你看看是否有帮助。

解决方案 »

  1.   

    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    this.sqlDataAdapter1.Fill(this.dataSet11,1,0,"Customers");
    } private void cmdback_Click(object sender, System.EventArgs e)
    {
    this.BindingContext[this.dataSet11,"Customers"].Position--; this.lstCustID.SelectedIndex=this.BindingContext[this.dataSet11,"Customers"].Position; } private void cmdnext_Click(object sender, System.EventArgs e)
    {
    this.BindingContext[this.dataSet11,"Customers"].Position++;
                
       this.lstCustID.SelectedIndex=this.BindingContext[this.dataSet11,"Customers"].Position; } private void lstCustID_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    this.BindingContext[this.dataSet11,"Customers"].Position=this.lstCustID.SelectedIndex; } private void cmdupdate_Click(object sender, System.EventArgs e)
    {
    this.sqlDataAdapter1.Update(this.dataSet11.Customers);

    }
    }
    }
    我是用SqlDataAdapter向导生成的,它会自动生成SqlInsertCommand等四个命令文件,请问如何使用,是否是用SQL的命令使用
      

  2.   

    inert into table1(a,b,c) value(TextBox1.text,TextBox2.text,TextBox3.text)
      

  3.   

    hehe  :)添加一个Command对象,COMMANDTEXT是带三个参数类型为INSTER。
    在适当的事件中执行他好了:)