新人学习C#,求解大神SqlDataAdapter填表问题

一线Jon 2019-07-12 01:09:01
第一张表[StuInfo]没问题, 都能读出来, 第二张表[StuCourse]没有数据....
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int current = 0;
DataSet dsStu = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "学生信息管理";
this.txtStuID.ReadOnly = true;
}

private void BtnRead_Click(object sender, EventArgs e)
{
string conStr = "Server=(local);Database=test;integrated security=true";
//SqlConnection cn = new SqlConnection(conStr);
SqlDataAdapter sda = new SqlDataAdapter("select * from StuInfo", conStr);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from StuCourse", conStr);

sda.Fill(dsStu,"StuInfo");
sda1.Fill(dsStu,"StuCourse");
ShowInfo();
}
private void ShowInfo()
{
this.txtStuID.Text = dsStu.Tables[0].Rows[current][0].ToString().Trim();
this.txtStuName.Text = dsStu.Tables["StuInfo"].Rows[current][1].ToString().Trim();
if (Convert.ToInt16(dsStu.Tables["StuInfo"].Rows[current][2]) == 1)
{
this.chkStuSexM.Checked = true;
this.chkStuSexF.Checked = false;
}
else
{
this.chkStuSexM.Checked = false;
this.chkStuSexF.Checked = true;
}
this.dtpStuBirth.Value = (DateTime)dsStu.Tables["StuInfo"].Rows[current][3];
this.txtStuAdd.Text = dsStu.Tables["StuInfo"].Rows[current][4].ToString().Trim();
this.txtStuTel.Text = dsStu.Tables["StuInfo"].Rows[current][5].ToString().Trim();
this.dtpStuAD.Value = (DateTime)dsStu.Tables["StuCourse"].Rows[current][1];
this.txtStuMajor.Text = dsStu.Tables["StuCourse"].Rows[current][2].ToString().Trim();

}
}
}

看了一下第二张表[StuCourse]的Rows.Count, 是0, 也就是什么都没读到..

麻烦大神帮忙看看是哪里的问题, 万分感谢!
...全文
140 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
一线Jon 2019-07-14
  • 打赏
  • 举报
回复
引用 7 楼 XBodhi. 的回复:
虽然你的代码 不规范,在你的基础给你改了下 。


namespace SQL
{
public partial class Form1 : Form
{
private int current = 0;

DataTable dtStuInfo = new DataTable();
DataTable dtStuCourse = new DataTable();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.Text = "学生信息管理";
this.txtStuID.ReadOnly = true;
}

private void BtnRead_Click(object sender, EventArgs e)
{
string conStr = "Server=(local);Database=test;integrated security=true";
//SqlConnection cn = new SqlConnection(conStr);
SqlDataAdapter sda = new SqlDataAdapter("select * from StuInfo", conStr);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from StuCourse", conStr);

sda.Fill(dtStuInfo);
sda1.Fill(dtStuCourse);
ShowInfo();
}
private void ShowInfo()
{
DataRow currentStuInfoRow = dtStuInfo.Rows[current];
DataRow currentStuCourseRow = dtStuCourse.Rows[current];

this.txtStuID.Text = currentStuInfoRow["StuID"].ToString().Trim();
this.txtStuName.Text = currentStuInfoRow[1].ToString().Trim();
if (Convert.ToInt16(currentStuInfoRow[2]) == 1)
{
this.chkStuSexM.Checked = true;
this.chkStuSexF.Checked = false;
}
else
{
this.chkStuSexM.Checked = false;
this.chkStuSexF.Checked = true;
}
this.dtpStuBirth.Value = (DateTime)currentStuInfoRow[3];
this.txtStuAdd.Text = currentStuInfoRow[4].ToString().Trim();
this.txtStuTel.Text = currentStuInfoRow[5].ToString().Trim();
this.dtpStuAD.Value = (DateTime)currentStuCourseRow[1];
this.txtStuMajor.Text = currentStuCourseRow[2].ToString().Trim();

}
}
}


看是否OK。

感谢, 已经解决, 刚刚开始学c#, 代码习惯与逻辑肯定有很多问题, 感谢大神指点
XBodhi. 2019-07-12
  • 打赏
  • 举报
回复
虽然你的代码 不规范,在你的基础给你改了下 。


namespace SQL
{
public partial class Form1 : Form
{
private int current = 0;

DataTable dtStuInfo = new DataTable();
DataTable dtStuCourse = new DataTable();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.Text = "学生信息管理";
this.txtStuID.ReadOnly = true;
}

private void BtnRead_Click(object sender, EventArgs e)
{
string conStr = "Server=(local);Database=test;integrated security=true";
//SqlConnection cn = new SqlConnection(conStr);
SqlDataAdapter sda = new SqlDataAdapter("select * from StuInfo", conStr);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from StuCourse", conStr);

sda.Fill(dtStuInfo);
sda1.Fill(dtStuCourse);
ShowInfo();
}
private void ShowInfo()
{
DataRow currentStuInfoRow = dtStuInfo.Rows[current];
DataRow currentStuCourseRow = dtStuCourse.Rows[current];

this.txtStuID.Text = currentStuInfoRow["StuID"].ToString().Trim();
this.txtStuName.Text = currentStuInfoRow[1].ToString().Trim();
if (Convert.ToInt16(currentStuInfoRow[2]) == 1)
{
this.chkStuSexM.Checked = true;
this.chkStuSexF.Checked = false;
}
else
{
this.chkStuSexM.Checked = false;
this.chkStuSexF.Checked = true;
}
this.dtpStuBirth.Value = (DateTime)currentStuInfoRow[3];
this.txtStuAdd.Text = currentStuInfoRow[4].ToString().Trim();
this.txtStuTel.Text = currentStuInfoRow[5].ToString().Trim();
this.dtpStuAD.Value = (DateTime)currentStuCourseRow[1];
this.txtStuMajor.Text = currentStuCourseRow[2].ToString().Trim();

}
}
}


看是否OK。
XBodhi. 2019-07-12
  • 打赏
  • 举报
回复
你代码写错了。

string conStr = "Server=(local);Database=test;integrated security=true";
//SqlConnection cn = new SqlConnection(conStr);
SqlDataAdapter sda = new SqlDataAdapter("select * from StuInfo", conStr);
SqlDataAdapter sda1 = new SqlDataAdapter("select * from StuCourse", conStr);

sda.Fill(dsStu,"StuInfo");
sda1.Fill(dsStu,"StuCourse");
ShowInfo();

这里写错了
一线Jon 2019-07-12
  • 打赏
  • 举报
回复
解决了, 之前将数据库文件建立并保存在笔记本的SD插槽里面的SD卡上的, 移植到本地硬盘上就解决了
ManBOyyy 2019-07-12
  • 打赏
  • 举报
回复
你在這裡打斷點 sda1.Fill(dsStu,"StuCourse");然後dsStu看看這個有木有數據
一线Jon 2019-07-12
  • 打赏
  • 举报
回复
引用 1 楼 ManBOyyy 的回复:
你在數據庫查詢select * from StuCourse 有木有數據啊


有数据的
ManBOyyy 2019-07-12
  • 打赏
  • 举报
回复
current這是等於幾?
ManBOyyy 2019-07-12
  • 打赏
  • 举报
回复
你在數據庫查詢select * from StuCourse 有木有數據啊

110,535

社区成员

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

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

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