如何在C#.net中使用ADO.net将表中可用的数据添加到窗体的文本框 [英] how to add the data available in a table to a textbox of a form using ADO.net in C#.net
本文介绍了如何在C#.net中使用ADO.net将表中可用的数据添加到窗体的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C#.net的初学者,我想构建一个简单的表单,在其中我要在文本框中显示数据,该文本框在SQLSERVER 2008数据库中可用,我通过使用断开连接的ado.net并显示了列名来尝试了它代替数据
[marcus:已删除的电子邮件]
i am beginner in C#.net i want to build a simple form where i want to display the data in the textbox which available in database of SQLSERVER 2008 i tried it by using ado.net of disconnected architecture and column name is displayed instead of data
[marcus: removed email]
推荐答案
在这里,我为您提供了一个使用SqlCommand
和DataReader
的ADO.NET的非常简单的示例:
Here, I made you a very simple example of ADO.NET usingSqlCommand
andDataReader
:
try
{
SqlConnection sqlconn = new SqlConnection(connString);
sqlconn.Open();
SqlCommand sqlcmd = new SqlCommand("SELECT fistname, lastname FROM Users",
sqlconn);
sqlcmd.CommandType = CommandType.Text;
SqlDataReader dr = sqlcmd.ExecuteReader();
while(dr.Read())
{
txtFirstname.Text = dr[0].ToString();
txtLastname.Text = dr[1].ToString();
}
}
catch(Exception ex)
{
//log ex
}
finally
{
sqlconn.Close();
sqlcmd.Dispose();
sqlconn = null;
}
问候,
Eduard
Regards,
Eduard
您可以使用已连接/已断开连接的体系结构进行操作.
连接的体系结构:数据读取器 [断开连接的体系结构:数据集 [
You can do it using Connected/disconnected architecture.
Connected architecture: data reader[^]
Disconnected architecture: dataset[^]
尝试以下示例
try this example
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection("Data Source=OMXOKLSFD0885\SQLEXPRESS;Initial Catalog=omx;Persist Security Info=True;");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * from emp WHERE UserID = @username", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@username",user);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text = dt.Rows[0]["name"].ToString();
TextBox2.Text = dt.Rows[0]["emailid"].ToString();
Label1.Text = dt.Rows[0]["dob"].ToString();
Label2.Text = dt.Rows[0]["Deptt."].ToString();
}
connection.Close();
}
这篇关于如何在C#.net中使用ADO.net将表中可用的数据添加到窗体的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文