【发布时间】:2016-06-02 16:15:33
【问题描述】:
我正在使用 C# 在 Visual Studio 中工作。我需要从 SQL Server 中的不同数据库表中获取数据到 Visual Studio 表单上的文本框中。数据库表有几行和几列。价值观是性格和真实的价值观。我正在使用下面的代码。我没有得到文本框的真实值。我没有收到任何错误和空文本框。我可以得到字符值。有人可以帮忙吗?谢谢。
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SqlConnection con = new SqlConnection("DataSource=192.146.1,5,1433;Network Library=DBMSSOCN;Initial Catalog=Estimator;User ID=id;Password=password;Trusted_Connection=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from Table1", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textBox1.Text = dr.GetString(1);
textBox2.Text = dr.GetString(2);
textBox2.Text = dr.GetString(3);
}
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
【问题讨论】:
-
调试代码时是否进入while循环?
-
是的,它进入了while循环,谢谢。
-
不要在 www 上显示您的公共 IP 地址。请编辑您的问题并将其更改为其他内容。喜欢 xxx.xxx.xxx.xxx
标签: c# sql-server