from窗体用C#对SQL server数据库对dategrid view的删除添加的显示结果出不来

weixin_45067850 2019-05-14 09:30:51
public void but添加_Click(object sender, EventArgs e) { int 学生学号 = Int32.Parse(textid1.Text); string 学生姓名 = textname.Text; string 院系 = textclass2.Text; string 班级 = textclass3.Text; int 班级人数 = Int32.Parse(textclassnum1.Text); if (textid1.Text=="" || string.IsNullOrEmpty(学生姓名)|| string.IsNullOrEmpty(院系 ) || string.IsNullOrEmpty(班级) ||textclassnum1.Text=="") { MessageBox.Show("用户名或密码不能为空!"); return; } SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder(); scsb.DataSource = "LAPTOP-7DRB565C"; scsb.IntegratedSecurity = true; scsb.InitialCatalog = "教材管理系统后台"; SqlConnection conn = new SqlConnection(scsb.ToString()); if (conn.State == System.Data.ConnectionState.Closed) conn.Open(); string strSQL = @"insert into studentinfo (学生学号,学生姓名,院系,班级,班级人数) values ('" + textid1.Text + "','" + textname.Text + "','" + textclass2.Text + "','" + textclass3.Text + "','" + textclassnum1.Text + "')"; SqlCommand comm = new SqlCommand(strSQL, conn); SqlDataAdapter da = new SqlDataAdapter(comm); DataSet ds = new DataSet(); int n = da.Fill(ds, "studentinfoinfo"); if (n != 0) { textid1.Text = ""; textname.Text = ""; textclass2.Text = ""; textclass3.Text = ""; textclassnum1.Text = ""; } else if (textname.TextLength > 16) { MessageBox.Show("学生姓名太长,我怕你记不住,请换个短的吧!", "提示"); } else if (textid1.TextLength > 16) { MessageBox.Show("学号太长,请换个短的吧!", "提示"); } else if (conn.State == System.Data.ConnectionState.Open) { MessageBox.Show("注册成功!"); conn.Close(); SqlConnection conn1 = new SqlConnection(scsb.ToString()); conn1.Open(); string strsql = @"SELECT 学生学号,学生姓名,班级,班级人数 FROM [dbo].[studentinfo]"; SqlCommand comm1 = new SqlCommand(strsql, conn1); SqlDataAdapter sda = new SqlDataAdapter(comm1); DataSet de = new DataSet(); dataGridView1.DataSource = sda.Fill(de, "studentinfo"); } 没有显示结果
...全文
112 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉普赛的歌 2019-05-17
  • 打赏
  • 举报
回复
引用 3 楼 weixin_45067850 的回复:
[quote=引用 1 楼 吉普赛的歌的回复:]单步跟踪,看下最终SQL, 放SQL server 中直接执行
怎么单步跟踪呢,应该是哪些命令啊?[/quote] https://blog.csdn.net/yenange/article/details/90297213
weixin_45067850 2019-05-16
  • 打赏
  • 举报
回复
数据库里面有的内容但是没有显示是为什么?
weixin_45067850 2019-05-16
  • 打赏
  • 举报
回复
还是没有办法实现,是怎么回事啊?
lich2005 2019-05-16
  • 打赏
  • 举报
回复
SqlCommand comm = new SqlCommand(strSQL, conn) comm .ExecuteNonQuery();
lich2005 2019-05-16
  • 打赏
  • 举报
回复
你这代码中没有执行命令的代码,也就是说 string strSQL = @"insert into studentinfo (学生学号,学生姓名,院系,班级,班级人数) values ('" + textid1.Text + "','" + textname.Text + "','" + textclass2.Text + "','" + textclass3.Text + "','" + textclassnum1.Text + "')"; 这段插入命令并没有执行,你可以在数据库中反查一下,数据库中应该没有写入你的数据,自然也就显示不出来了。 如果要执行的话,会写入类似代码
weixin_45067850 2019-05-16
  • 打赏
  • 举报
回复
引用 1 楼 吉普赛的歌的回复:
单步跟踪,看下最终SQL, 放SQL server 中直接执行
怎么单步跟踪呢,应该是哪些命令啊?
weixin_45067850 2019-05-16
  • 打赏
  • 举报
回复
怎么单步跟踪呢,应该是哪些命令啊?
吉普赛的歌 2019-05-15
  • 打赏
  • 举报
回复
单步跟踪,看下最终SQL, 放SQL server 中直接执行

6,129

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 新技术前沿
社区管理员
  • 新技术前沿社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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