C# 如何从数据库表中获取数据,然后插入另一个表中

yghuaa 2013-04-24 09:03:28
获取表一的一条记录Username和表2的一条记录Groupname,插入表3中?
...全文
1364 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yghuaa 2013-04-24
  • 打赏
  • 举报
回复
太爱你了 帮我解决这个问题
瑞卡哥哥 2013-04-24
  • 打赏
  • 举报
回复 1
insert into [GROUPUSER](USERID,GROUPID) select [USER].USERID,[GROUP].GROUPID from [USER],[GROUP] group user 都是sql的关键字
瑞卡哥哥 2013-04-24
  • 打赏
  • 举报
回复
嗯 group 是关键字 要加中括号
yghuaa 2013-04-24
  • 打赏
  • 举报
回复
string strCon = ConfigurationManager.ConnectionStrings["TPVConnectionString"].ToString(); SqlConnection con = new SqlConnection(strCon); con.Open(); string strSql = "select * from[USER] where [LOGONID]='"+txtAD.Text.Trim()+"'"; SqlCommand com = new SqlCommand(strSql,con); SqlDataReader dr = com.ExecuteReader(); if(dr.Read()) { string strconn = ConfigurationManager.ConnectionStrings["TPVConnectionString"].ToString(); SqlConnection conn = new SqlConnection(strconn); conn.Open(); lblName.Text=dr["USERNAME"].ToString(); lblName.Visible = true; string strSqlInsert = @"insert into [GROUPUSER](USERID,GROUPID) select USER.USERID,GROUP.GROUPID from USER,GROUP where GROUPNAME='"+ddlGroup.SelectedValue+"',USERNAME='"+lblName.Text+"'"; SqlCommand comm = new SqlCommand(strSqlInsert,conn); comm.ExecuteNonQuery(); conn.Close(); Response.Write("<script>alert('添加权限成功!');</script>"); } else { lblName.Text = "不存在此AD!"; lblName.Visible = true; btnOk.Enabled = false; } con.Close(); 这个运行后会显示: 关键字 'GROUP' 附近有语法错误。
yghuaa 2013-04-24
  • 打赏
  • 举报
回复
string strCon = ConfigurationManager.ConnectionStrings["TPVConnectionString"].ToString(); SqlConnection con = new SqlConnection(strCon); con.Open(); string strSql = "select * from[USER] where [LOGONID]='"+txtAD.Text.Trim()+"'"; SqlCommand com = new SqlCommand(strSql,con); SqlDataReader dr = com.ExecuteReader(); if(dr.Read()) { string strconn = ConfigurationManager.ConnectionStrings["TPVConnectionString"].ToString(); SqlConnection conn = new SqlConnection(strconn); conn.Open(); lblName.Text=dr["USERNAME"].ToString(); lblName.Visible = true; string strSqlInsert = @"insert into [GROUPUSER](USERID,GROUPID) select USER.USERID,GROUP.GROUPID from USER,GROUP where GROUPNAME='"+ddlGroup.SelectedValue+"',USERNAME='"+lblName.Text+"'"; SqlCommand comm = new SqlCommand(strSqlInsert,conn); comm.ExecuteNonQuery(); conn.Close(); Response.Write("<script>alert('添加权限成功!');</script>"); } else { lblName.Text = "不存在此AD!"; lblName.Visible = true; btnOk.Enabled = false; } con.Close(); 这个运行后会显示: 关键字 'GROUP' 附近有语法错误。
maxldwy 2013-04-24
  • 打赏
  • 举报
回复
select 表1的UserName from 表1 where 条件.. string 字段1 = UserName select 表2的GroupName from 表2 where 条件.. string 字段2 = GroupName insert into 表3 Value(字段1,字段2) ;
瑞卡哥哥 2013-04-24
  • 打赏
  • 举报
回复
直接在数据库 select 表1.UserName,表2.GroupName where 1=1 into 表3(col1,col2)
gxingmin 2013-04-24
  • 打赏
  • 举报
回复
insert into 表3(字段1,字段2) select  表1.UserName,表2.GroupName from 表1,表2 where ...

110,530

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧