(一)
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参数是插入语句呢?
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参数是插入语句呢?
解决方案 »
- XML解析错误:未找到元素
- 100分求一个时间往后推算函数
- 修改task scheduler中某task的trigger的时间
- ThreadPool CPU 占用很高的问题
- 可用积分-的了,c#winform如何在没有c#的环境运行的问题--只能0分贴了
- 为什么在本地可以,一旦上传服务器,老会出现这个问题WebForm_PostBackOptions未定义,急
- 高分求助:如何实现在线考试到时间系统自动提交的问题(急!在线等)
- c# textbox的内容怎么通过streamwriter实时自动保存到txt文件里
- 有高手能解决这个问题吗?
- 我在编译时出错了,请大家帮我看看,应该怎么编译?
- c# button控件引用的gif怎么不动?
- winfrom 中 statusBar的ICON图标如何动态更改
SqlCommand comm = new SqlCommand(Sql, sqlcon); //Sql参数可以不是查询语句吗?直接用com.ExecuteNonQuery(sql)就ok了,用DataAdapter基本都和Datase一起用来取数据集的
如下代码就可以插入: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();请问这与(二)有什么不同吗?多谢指教