3

我正在创建一个预订管理系统,但在尝试从 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();
4

5 回答 5

9

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按钮时更改 texboxes 中的数据,您应该创建并使用自己的模型,或者您可以将数据存储在 DataTable 和如果您愿意,请稍后参考。

于 2013-05-15T12:34:47.100 回答
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();
}

确保查询中有数据:select * from Pending_Tasks并且您正在使用“使用 System.Data.SqlClient;”

于 2013-05-15T12:38:09.773 回答
2

read = com.ExecuteReader()

SqlDataReader有一个函数Read()可以从查询结果中读取下一行,并返回bool是否找到下一行要读取。所以你需要在你真正从你的读者那里得到列之前检查一下(它总是只得到当前的行Read())。while(read.Read())或者,如果您的查询返回多行,最好进行循环。

于 2013-05-15T12:33:08.550 回答
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();
于 2016-10-26T06:20:41.287 回答
0

建立连接并打开它。

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 也添加它;

于 2017-02-28T07:13:01.733 回答