在SQL server数据库中若想根据学生学号(在textbox1.text中)的内容在数据库的表stuInfo中提取姓名,年龄,专业,并把
学号,姓名,年龄,专业插入到表useInfo中,我的代码如下:
string strSql2 = "";
strSql2 = "select stuName,stuAge,stuMajor from stuInfo where stuNo='" + textBox1.Text + "' ";
SqlConnection conn4 = new SqlConnection("server=.;database=ParkingSystem;uid=sa;pwd=000000");
conn4.Open();
SqlCommand comm4 = new SqlCommand(strSql2, conn4);
SqlConnection conn3 = new SqlConnection();
conn3.ConnectionString = "server=.;database=ParkingSystem;uid=sa;pwd=000000";
string sql3 = "insert into useInfo(stuNo,stuName,stuAge,stuMajor ) values ('" + textBox1.Text + "','{0}','{1}','{2}')";
conn3.Open();
SqlCommand cmd3 = new SqlCommand(sql3, conn3);
cmd3.ExecuteNonQuery();
conn3.Close();
请问正确吗?怎么改正呢?
学号,姓名,年龄,专业插入到表useInfo中,我的代码如下:
string strSql2 = "";
strSql2 = "select stuName,stuAge,stuMajor from stuInfo where stuNo='" + textBox1.Text + "' ";
SqlConnection conn4 = new SqlConnection("server=.;database=ParkingSystem;uid=sa;pwd=000000");
conn4.Open();
SqlCommand comm4 = new SqlCommand(strSql2, conn4);
SqlConnection conn3 = new SqlConnection();
conn3.ConnectionString = "server=.;database=ParkingSystem;uid=sa;pwd=000000";
string sql3 = "insert into useInfo(stuNo,stuName,stuAge,stuMajor ) values ('" + textBox1.Text + "','{0}','{1}','{2}')";
conn3.Open();
SqlCommand cmd3 = new SqlCommand(sql3, conn3);
cmd3.ExecuteNonQuery();
conn3.Close();
请问正确吗?怎么改正呢?
解决方案 »
- 导入EXCEL表的问题~~建立到服务器的连接时发生错误
- 在存储过程中,如何从一个服务器的数据库里读出表的记录,更新到另一个服务器的数据库指定表?
- 想问下关于数据库的问题啊,我怎么往视图里增加一个自增字段名?这个字段名在与该视图相关联的表中是不存在的。
- 数据库空间大小问题咨询
- 求一SQL语句,对于你来说可能不难!:)
- 有关字符串执行的问题 exec('select * into #ware from jinhuflag') 急!
- [50分]如何把EXCEL文件导入远程数据库作为一个新表
- 关于复杂行列转换的问题.急!
- 安装SQLSERVER2000出现"许可证模式的选择"应如何设置.
- 如何使ORACLE的存储过程返回结果集?
- 有没有跟校内开心一样的学校和城市数据库
- 求教SQL语句,具体见帖子内容,谢谢。
txtAge.Text = tb.Rows[0].ItemArray[2].ToString();
txtMajor.text=tb.Row[0].ItemArray[3].Tostring();
才能提取
conn3.ConnectionString = "server=.;database=ParkingSystem;uid=sa;pwd=000000";
string sql3 = "insert into useInfo(stuNo,stuName,stuAge,stuMajor )
select stuName,stuAge,stuMajor from stuInfo where stuNo='" + textBox1.Text + "' ";
conn3.Open();
SqlCommand cmd3 = new SqlCommand(sql3, conn3);
cmd3.ExecuteNonQuery();
conn3.Close();只用一条sql就ok了
string sql3 = "insert into useInfo(stuNo,stuName,stuAge,stuMajor )
select stuName,stuAge,stuMajor from stuInfo where stuNo='" + textBox1.Text + "' "; 一个SqlConnection对象就可以了