注册 登录
编程论坛 C# 论坛

C#中textbox1中显示数据库中数据时出错

于纪东 发布于 2015-05-17 11:39, 534 次点击
   private void button5_Click(object sender, EventArgs e)
        {
            string conStr = @"Data Source=于纪东-PC; Initial Catalog = 铝板生产线自动测厚系统数据库;
            User ID = sa; Password = sasa;Integrated Security=true";
            SqlConnection connection = new SqlConnection(conStr);
            SqlCommand cmd = connection.CreateCommand();
             = "SELECT 电压,厚度,日期 FROM 数值表";
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);
            adapter.InsertCommand = cmdBuilder.GetInsertCommand();
            
            
            adapter.Fill(ds);
            
            DataTable dt = ds.Tables["Table"];
            dt.Rows.Add("112.21", "21.12", "2015-05-17 10:21:32");
            dt.Rows.Add("115.43", "22.22", "2015-05-17 10:23:12");
            dt.Rows.Add("120.12", "23.02", "2015-05-17 10:24:20");
            dt.Rows.Add("124.43", "24.52", "2015-05-17 10:27:21");
            dt.Rows.Add("113.23", "21.56", "2015-05-17 10:28:20");
            dt.Rows.Add("112.34", "21.15", "2015-05-17 10:28:32");
            dt.Rows.Add("115.21", "21.98", "2015-05-17 10:29:10");
            dt.Rows.Add("109.67", "20.89", "2015-05-17 10:30:12");
            dt.Rows.Add("110.23", "21.01", "2015-05-17 10:31:32");
            
            this.textBox1.Text = ds.Tables["电压"].ToString();
            this.textBox2.Text = ds.Tables["厚度"].ToString();
只有本站会员才能查看附件,请 登录

            this.textBox3.Text = ds.Tables["日期"].ToString();
            adapter.Update(ds);
        }
    }
}
2 回复
#2
Maick2015-05-18 11:26
DataTable dt = ds.Tables["Table"];
改成 DataTable dt = ds.Tables[0]; 试试
#3
Maick2015-05-19 10:16
ds.Tables["电压"]??
ds.Tables[0].rows[index]["电压"]这样访问的吧
1