SQL Sever在C#中将数据读取到数组

时间:2016-12-20 11:39:17

标签: c# sql-server xaml

您好我正在尝试在我的应用程序中创建自动填充。 但有些如何不填充数组。有人能帮我吗? 我是C#的新手,所以我很抱歉愚蠢的错误。

private void autonrTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        try
        {


            int i = 0;
            var check[i];
            using (var con2 = new SqlConnection(@"Data Source=DESKTOP-RSEBNR7;Initial Catalog=AudiDealer;Integrated Security=True"))
            using (var cmd2 = new SqlCommand("SELECT * FROM auto where autonr = " + autonrTextBox.Text, con2))
            {
                con2.Open();
                check = cmd2.ExecuteScalar();
                con2.Close();
                autonrTextBox.Text = check[0];
                kentekenTextBox.Text = check[1];
                merkTextBox.Text = check[2];
                modelTextBox.Text = check[3];
                kleurTextBox.Text = check[4];
                categorieTextBox.Text = check[5];
                pkSTextBox.Text = check[6];
                apkTextBox.Text = check[7];
                kilometerstandTextBox.Text = check[8];
                bijtellingTextBox.Text = check[9];
                energielabelTextBox.Text = check[10];
            }
        }
        catch
        {
            MessageBox.Show("Dit Auto nummer komt niet voor in de database. controleer deze en probeer opnieuw","Error");
        }
    }

2 个答案:

答案 0 :(得分:2)

即使您想要阅读记录,您也必须使用���I9d� V?r��U8�&���B$̌�83 ExecuteReader()返回单一值):

ExecuteScalar

答案 1 :(得分:1)

您必须使用ExecuteReader。 ExecuteScalar仅返回单个数据。例如:count,sum,min,max。聚合函数

https://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx