我正在创建一个预订管理系统,我在尝试从SQL数据库中获取数据并插入到我的应用程序的一组文本框中时遇到问题。
我希望在DataGridView中显示单击按钮时的客户详细信息,但是当我单击该按钮时,应用程序会抛出异常,并显示以下错误消息;
没有数据时无效尝试读取。
我附加了screenshot屏幕,我想要查看客户详细信息,以及按钮的代码,最终会在相应的文本框中显示客户详细信息。任何帮助将不胜感激!
SqlConnection sc = new SqlConnection("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True");
SqlCommand com = new SqlCommand();
com.Connection = sc;
sc.Open();
SqlDataReader read = (null);
com.CommandText = ("select * from Pending_Tasks");
read = com.ExecuteReader();
CustID.Text = (read["Customer_ID"].ToString());
CustName.Text = (read["Customer_Name"].ToString());
Add1.Text = (read["Address_1"].ToString());
Add2.Text = (read["Address_2"].ToString());
PostBox.Text = (read["Postcode"].ToString());
PassBox.Text = (read["Password"].ToString());
DatBox.Text = (read["Data_Important"].ToString());
LanNumb.Text = (read["Landline"].ToString());
MobNumber.Text = (read["Mobile"].ToString());
FaultRep.Text = (read["Fault_Report"].ToString());
sc.Close();
答案 0 :(得分:6)
代码中缺少行reader.Read()
。你应该添加它。它是实际从数据库中读取数据的函数:
string conString = "Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True";
SqlConnection con = new SqlConnection(conString);
string selectSql = "select * from Pending_Tasks";
SqlCommand com = new SqlCommand(selectSql, con);
try
{
con.Open();
using (SqlDataReader read = cmd.ExecuteReader())
{
while(reader.Read())
{
CustID.Text = (read["Customer_ID"].ToString());
CustName.Text = (read["Customer_Name"].ToString());
Add1.Text = (read["Address_1"].ToString());
Add2.Text = (read["Address_2"].ToString());
PostBox.Text = (read["Postcode"].ToString());
PassBox.Text = (read["Password"].ToString());
DatBox.Text = (read["Data_Important"].ToString());
LanNumb.Text = (read["Landline"].ToString());
MobNumber.Text = (read["Mobile"].ToString());
FaultRep.Text = (read["Fault_Report"].ToString());
}
}
}
finally
{
con.Close();
}
编辑:此代码可以正常运行,假设您要将最后一条记录写入文本框。如果要应用其他方案,例如,在单击Next
按钮时读取数据库中的所有记录并更改texbox中的数据,则应创建并使用自己的模型,或者可以存储DataTable中的数据,如果您愿意,可以在以后引用它们。
答案 1 :(得分:3)
using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True"))
{
SqlCommand command =
new SqlCommand("select * from Pending_Tasks WHERE CustomerId=...", connection);
connection.Open();
SqlDataReader read= command.ExecuteReader();
while (read.Read())
{
CustID.Text = (read["Customer_ID"].ToString());
CustName.Text = (read["Customer_Name"].ToString());
Add1.Text = (read["Address_1"].ToString());
Add2.Text = (read["Address_2"].ToString());
PostBox.Text = (read["Postcode"].ToString());
PassBox.Text = (read["Password"].ToString());
DatBox.Text = (read["Data_Important"].ToString());
LanNumb.Text = (read["Landline"].ToString());
MobNumber.Text = (read["Mobile"].ToString());
FaultRep.Text = (read["Fault_Report"].ToString());
}
read.Close();
}
确保查询中包含数据:从Pending_Tasks中选择* ,并使用"使用System.Data.SqlClient;"
答案 2 :(得分:2)
read = com.ExecuteReader()
SqlDataReader
有一个函数Read()
,它会从查询结果中读取下一行,并返回bool
是否找到要读取的下一行。所以你需要先检查一下,然后才能从阅读器中获取列(总是得到Read()
得到的当前行)。或者,如果查询返回多行,最好生成一个循环while(read.Read())
。
答案 3 :(得分:0)
如果要显示从数据库到文本框的单值访问,请参阅以下代码:
SqlConnection con=new SqlConnection("connection string");
SqlCommand cmd=new SqlConnection(SqlQuery,Con);
Con.Open();
TextBox1.Text=cmd.ExecuteScalar();
Con.Close();
或
SqlConnection con=new SqlConnection("connection string");
SqlCommand cmd=new SqlConnection(SqlQuery,Con);
Con.Open();
SqlDataReader dr=new SqlDataReadr();
dr=cmd.Executereader();
if(dr.read())
{
TextBox1.Text=dr.GetValue(0).Tostring();
}
Con.Close();
答案 4 :(得分:-1)
建立连接并打开它。
$ git push -u
Branch <branch_name> set up to track remote branch <branch_name> from origin.
Everything up-to-date
编写选择查询:
con = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=<database_name>)));User Id =<userid>; Password =<password>");
con.Open();
创建命令对象:
string sql = "select * from Pending_Tasks";
执行命令并将结果放入对象中以进行读取。
OracleCommand cmd = new OracleCommand(sql, con);
现在开始阅读。
OracleDataReader r = cmd.ExecuteReader();
使用 Oracle.ManagedDataAccess.Client;
添加此项