【问题标题】:From combo box to textbox从组合框到文本框
【发布时间】:2017-09-21 11:02:00
【问题描述】:

我无法弄清楚我的代码出了什么问题或者解决方案可能是什么 我也不知道如何编写和获取要放回日期时间选择器的日期时间。

private void cbxProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            string constring = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\hannes.corbett\\Desktop\\Barcode Scanning\\Barcode Scanning\\BarcodeDB.mdf\"; Integrated Security = True";
            string Query = "SELECT * FROM Products where Name='" + cbxProducts.Text + "' ; ";
            SqlConnection conDataBase = new SqlConnection(constring);
            SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
            SqlDataReader myReader;
            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();

                string sBarcode = myReader.GetString("Barcodes");
                string sName = myReader.GetString("Name");
                var sDate = myReader.GetDateTime("EDate");
                string sQuantity = myReader.GetInt32("Quantity")).ToString();
                string sPrice = myReader.GetInt32("Price")).ToString();
                tbxBar.Text = sBarcode;
                tbxName.Text = sName;
                sDate = dateDate.Value;
                tbxPrice.Text = sPrice;
                tbxQua.Text = sQuantity;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

编辑:Error Message

编辑 2:我收到错误消息

“当没有数据可用时进行了无效的读取尝试”

我的数据库中的所有字符串都有数据,但我仍然收到此错误

【问题讨论】:

  • 您能否提供确切的错误信息?我认为string sQuantity = myReader.GetInt32("Quantity")).ToString();string sPrice = myReader.GetInt32("Price")).ToString(); 都是错误的。检查您的数据库中有哪些数据类型。
  • 有了这些信息,我们只能猜测。我的猜测是价格或数量或两者都不是 int。
  • 无法从字符串转换为 int 是我悬停时得到的结果,但我无法运行代码,因为“条形码”并且所有这些都带有红色下划线
  • 您应该始终使用parameterized queries。这种字符串连接对SQL Injection 攻击开放。还可以使用using 语句自动处理您的连接和命令
  • 您的问题是关于为什么您收到错误“无法从字符串转换为 int”,答案是因为方法 GetStringGetDateTimeGetInt32 适用于字段索引的字段名称。接受答案并尝试自己解决程序的解决方案或提出有关按名称获取字段值的新问题(此处也已回答)

标签: c# sql datetime datetimepicker


【解决方案1】:

您必须使用列的索引而不是名称

string sBarcode = myReader.GetString(IndexOfBarcodesColumn);

类似的东西

string sBarcode = myReader.GetString(0);
string sName = myReader.GetString(1);
var sDate = myReader.GetDateTime(2);
string sQuantity = myReader.GetInt32(3).ToString();
string sPrice = myReader.GetInt32(4).ToString();

或者您可以直接从阅读器中使用字段名称

string sBarcode = myReader.Item["Barcodes"];
string sName = myReader.Item["Name"];
var sDate = myReader.Item["EDate"];
string sQuantity = myReader.Item["Quantity"];
string sPrice = myReader.Item["Price"];

【讨论】:

  • 好的,我是数据库新手,如何找到不同列的索引?
  • 如果你使用SELECT * FROM Products,列的顺序是在数据库中创建的,但是你可以使用SELECT Barcodes, Name, EDate, Quantity, Price FROM Products这样的东西按照你想要的顺序获取列,所以条形码的索引为0,名称索引 1 等等..
  • 如果这回答了您的问题,请接受回答
  • @MtwStark 我认为我们都是对的。看看this。您的语法似乎是 VB.NET 和 C# 的混合体。
  • @MatSnow 你是对的,我使用 vb 表示法作为括号而不是 c# 方括号。我现在已经更正了! :) 我使用 vb,这是一个 ctrl-c/ctrl-v 错误 ;)
【解决方案2】:

像这样它应该与列名一起使用,或者如果您想使用 columnIndex,请按照@MtwStark 在他的回答中描述的方式进行操作。

string sBarcode = myReader["Barcodes"].ToString();
string sName = myReader["Name"].ToString();
var sDate = myReader["EDate"];
string sQuantity = myReader["Quantity"].ToString();
string sPrice = myReader["Price"].ToString();

关于以下错误信息

“当没有数据可用时进行了无效的读取尝试”

您必须先致电myReader.Read()。 (就像有人在this question 中问的那样)

while (myReader.Read()) {
    //do here whatever you want with the records returned from myReader
    string sBarcode = myReader["Barcodes"].ToString();
    string sName = myReader["Name"].ToString();
    var sDate = myReader["EDate"];
    string sQuantity = myReader["Quantity"].ToString();
    string sPrice = myReader["Price"].ToString();
    ...
}

【讨论】:

  • 当我这样做时,myReader 得到带有下划线的 read,并显示“需要方法名称”
  • 已修复它,以便我可以运行代码,但是当我尝试从我的组合框中进行选择时,我收到错误“C# 在没有可用数据时进行读取的尝试无效”
  • 如果您的原始问题得到解答,您应该在此处接受答案并创建一个新问题。
  • @MtwStark 是的,应该可以。这同样适用于 VB.NET。我从不写myReader.Item("FieldName") 我总是用myReader("FieldName")
  • @HannesCorbett 问题不是“请让我的程序正常工作”而是“为什么我会收到这个错误?”所以回答了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
相关资源
最近更新 更多