我创建一个预订管理系统,我有试图从SQL数据库获取数据并插入到一组我的应用程序的文本框的问题。
我想要显示的客户详细信息,当按钮被点击时,在一个DataGridView,但是当我点击该按钮,应用程序将引发与以下错误消息的异常;
无效的尝试时不存在数据读取。
我附上一个截图 ,我要查看客户详细信息屏幕,并为按钮,最终将显示在相应的文本框客户详细信息的代码。 任何帮助将不胜感激!
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();
该生产线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();
}
编辑:此代码假设你想要写的最后一条记录的文本框。 如果你想申请一个不同的场景,例如像以读取数据库中的所有记录,并更改texboxes数据,当您单击Next
按钮,你应该创建和使用自己的型号,或者您也可以在数据表中存储数据并指给他们,如果你想以后。
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();
}
请确保您有在查询数据:SELECT * FROM Pending_Tasks和您正在使用“使用System.Data.SqlClient的;”
read = com.ExecuteReader()
SqlDataReader
具有功能Read()
读取从查询结果的下一行,并返回一个bool
是否找到了下一行来读取或没有。 所以,你需要检查之前,你实际上是从你的读者(这永远只是获取当前行是得到列Read()
了)。 或者最好做一个循环while(read.Read())
如果您的查询返回多行。
如果你想显示从数据库中单值接入到文本框,请参阅下面的代码:
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();
进行连接并打开它。
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();
现在开始从中读取。
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();
添加这也采用Oracle.ManagedDataAccess.Client;