(一) 
         static void Main(string[] args)
        {
            string Str = "Data Source=localhost;Initial Catalog=stuinfo;Integrated Security=True";
            string Sql = "select * from information";
            SqlConnection sqlcon = new SqlConnection(Str);
            DataSet Mydata = new DataSet();
            SqlDataAdapter Myadapter = new SqlDataAdapter(Sql, sqlcon);            Myadapter.Fill(Mydata, "stutable");
            Sql = "insert into information values(@stuno,@stuname,@stumajor)";//该行代码
            SqlCommand comm = new SqlCommand(Sql, sqlcon);   //Sql参数可以不是查询语句吗?
            Myadapter.InsertCommand = comm;
            Myadapter.InsertCommand.Parameters.Add("@stuno", SqlDbType.VarChar, 8, "stuno");
            Myadapter.InsertCommand.Parameters.Add("@stuname", SqlDbType.VarChar, 8, "stuname");
            Myadapter.InsertCommand.Parameters.Add("@stumajor", SqlDbType.VarChar, 8, "stumajor");
            DataRow myDR = Mydata.Tables["stutable"].NewRow();
            myDR["stuno"] = "05";
            myDR["stuname"] = " 王敬华";
            myDR["stumajor"] = "信息工程学院";
            Mydata.Tables["stutable"].Rows.Add(myDR);
            Myadapter.Update(Mydata, "stutable");
            sqlcon.Close();
        }
       (二)
        static void Main(string[] args)
        {
            string Str = "Data Source=localhost;Initial Catalog=stuinfo;Integrated Security=True";
            string Sql = "select * from information";
            SqlConnection sqlcon = new SqlConnection(Str);
            DataSet Mydata = new DataSet();
            SqlDataAdapter Myadapter = new SqlDataAdapter(Sql, sqlcon);            Myadapter.Fill(Mydata, "stutable");
            Sql = "insert into information values('05','王敬华','信息工程学院')";//该行代码与
            SqlCommand comm = new SqlCommand(Sql, sqlcon);   //Sql参数可以不是查询语句吗?
            Myadapter.InsertCommand = comm;
            Myadapter.Update(Mydata, "stutable");
            sqlcon.Close();
        }
        为什么(一)中可以插入记录,而(二)却不可以,另外我看一些教材上说SqlCommand(Sql, sqlcon)中的sql参数 只能是查询参数,但为什么(一)中Sql参数是插入语句呢?

解决方案 »

  1.   

     Sql = "insert into information values(@stuno,@stuname,@stumajor)";//该行代码
      SqlCommand comm = new SqlCommand(Sql, sqlcon); //Sql参数可以不是查询语句吗?
    直接用com.ExecuteNonQuery(sql)就ok了,用DataAdapter基本都和Datase一起用来取数据集的
      

  2.   

    Sql参数增 删 改 查 都可以的!
      

  3.   

    这样是可以,但我就是想搞清楚像(二)那样为什么不可以
    如下代码就可以插入:string Str = "Data Source=localhost;Initial Catalog=stuinfo;Integrated Security=True";
                SqlConnection conn = new SqlConnection(Str);
                conn.Open();
                string comStr = "select * from information";
                SqlCommand comm = new SqlCommand(comStr, conn);
                SqlDataAdapter myDA = new SqlDataAdapter();            myDA.SelectCommand = comm;
                DataSet myDS = new DataSet();
                myDA.Fill(myDS, "information");            comm.CommandText = "DELETE FROM information WHERE stuno =  '05'";
                myDA.DeleteCommand = comm;
                myDA.Fill(myDS, "information");            comm.CommandText = "insert into information values('01','王敬华','信息工程学院')";
                myDA.InsertCommand = comm;
                myDA.Fill(myDS, "information");            myDA.Update(myDS, "information");
                conn.Close();
                myDS.Dispose();请问这与(二)有什么不同吗?多谢指教