从网上找了很多给数据表添加新行的方法,一个一个的试过来,基本都可以连接数据库,但是添加数据的时候,无一例外,全部失败。我想通过3个文本框,输入数据后,单击按钮,添加到表中。表名:student;字段:StudentNum,Sex,StudentAge。请高手帮忙实现以下。谢谢。下面是我找的几个方法(本人与初学者等同,各位高手别笑。)
private void button5_Click(object sender, EventArgs e)
        {
            string strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Documents\mjxxtk.mdf;Initial Catalog=mjxxtk.mdf;Integrated Security=SSPI";
            try
            {
//失败
                //SqlConnection sqlConn = new SqlConnection(strConn);
                //sqlConn.Open();                //StringBuilder bul = new StringBuilder();
                //bul.Append("insert into student(StudentNum,Sex,StudentAge) values('" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "') ");
                //SqlCommand cmd = new SqlCommand(bul.ToString(), sqlConn);
                //cmd.ExecuteNonQuery();//问题在这一句
                //sqlConn.Close();
                //MessageBox.Show("数据库连接成功!");                //失败
                //SqlConnection sqlConn = new SqlConnection(strConn);
                //SqlCommand comm = new SqlCommand();
                //sqlConn.Open(); MessageBox.Show("数据库连接成功!");                //SqlDataAdapter sqlReader = new SqlDataAdapter(strConn, sqlConn);
                //DataSet ds = new DataSet();
                //sqlReader.Fill(ds,"ans");
                //DataRow dr = ds.Tables["ans"].NewRow();
                //dr["StudentNum"] = "0001";MessageBox.Show("数据库连接成功!");
                //dr["TiMuXuHao"] = "01";
                //dr["AnswerCase"] = "1";
                //sqlConn.Close();//失败
                //SqlConnection sqlConn = new SqlConnection(strConn);
                //System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from student ", strConn);
                //System.Data.SqlClient.SqlCommandBuilder db = new System.Data.SqlClient.SqlCommandBuilder(da);
                //DataTable tb = new DataTable();
                //da.SelectCommand.Parameters.Add("@StudentNum", SqlDbType.NVarChar, 50).Value = textBox5.Text;
                //da.Fill(tb);
                //DataRow row;
                //if (tb.Rows.Count == 0)
                //{
                //    row = tb.Rows.Add();
                //    row["StudentNum"] = textBox5.Text;
                //}
                //else
                //{
                //    row = tb.Rows[0];
                //}
                //row["Sex"] = textBox6.Text;
                //row["StudentAge"] = textBox7.Text;                //失败
                //using (SqlCommand cmd = new SqlCommand("insert into student(StudentNum,Sex,StudentAge) values('" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "') ", new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Documents\mjxxtk.mdf;Initial Catalog=mjxxtk.mdf;Integrated Security=SSPI")))
                //{
                //    MessageBox.Show("数据库连接成功!");
                //    cmd.Connection.Open();
                //    cmd.ExecuteNonQuery();
                //}
            }
            catch
            {
                MessageBox.Show("数据库连接不成功!");            }
            //这个也失败了!!!!!!!!!!!!
            //string strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Documents\mjxxtk.mdf;Initial Catalog=mjxxtk.mdf;Integrated Security=SSPI";
            //SqlConnection sqlConn = new SqlConnection(strConn);
            //sqlConn.Open();
            ////执行SQL语句
            //String SQL = "Select * from student";
            //SqlDataAdapter sqlReader = new SqlDataAdapter(SQL, sqlConn);
            //SqlCommandBuilder MyBuilder = new SqlCommandBuilder(sqlReader);//创建/新对象SqlCommandBuilder ,用于生成更新数据的SQL语句
            //DataSet ds = new DataSet();
            //sqlReader.Fill(ds);
            //DataTable dt = ds.Tables[0];
            //dt.TableName = "test";
            //DataRow thisRow = dt.NewRow();
            //thisRow[0] = textBox5.Text; 
            //thisRow[1] = textBox6.Text;
            //thisRow[1] = textBox7.Text;
            //dt.Rows.Add(thisRow);//使用Rows集合的Add方法添加新行
            //sqlReader.Update(ds, "test");        }c#数据库添加数据sql

解决方案 »

  1.   

    参考:http://msdn.microsoft.com/ZH-CN/library/hf02d713(v=VS.110,d=hv.2).aspx
    自己传递链接字符串和SQL语句,对于新增应该是INSERT语句
    而这里有一段带参数的更新示例:http://msdn.microsoft.com/ZH-CN/library/1bb4559a(v=VS.110,d=hv.2).aspx
    你找的都是用SqlCommandBuilder来操作的,那个不好,特别是你这里是直接的数据新增,根本没有用到数据集的更新,直接操作SqlCommand才对。
      

  2.   

    是不是数据类型的原因,比如Sex是bit型,只能存0,1
      
    *****************************************************************************
    http://feiyun0112.cnblogs.com/
      

  3.   


    按例子修改代码:
                string strConn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\My Documents\mjxxtk.mdf;Initial Catalog=mjxxtk.mdf;Integrated Security=SSPI";
                string strCd = "insert into answercase(StudentNum,TiMuXuHao,AnswerCase) values('" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "') ";
                try
                {
                    using (SqlConnection connection = new SqlConnection(strConn))
                    {
                        SqlCommand command = new SqlCommand(strCd, connection);
                        command.Connection.Open(); MessageBox.Show("1!");
                        command.ExecuteNonQuery(); MessageBox.Show("2!");
                    }
                }
                catch
                {
                    MessageBox.Show("数据库连接不成功!");            }
    还是失败。郁闷啊
      

  4.   


    我建了一个小程序做实验的,为了方便,全部都是用的string类型。