-1

我想将 SQL Server 数据库中的数据从多列填充到多个文本框.. 我有一个代码可以只填充一个框.. 有人可以编辑我的代码吗...我想提取数据并将其显示在名称、地址、电话号码和日期...请帮助..此代码仅适用于一个文本框..

提前致谢

SqlConnection Conn = new SqlConnection(@"Data Source=rex;Initial Catalog=PersonalDetails;Integrated Security=True");
SqlCommand Comm1 = new SqlCommand("Select * From PersonalUsers ", Conn);

Conn.Open();
SqlDataReader DR1 = Comm1.ExecuteReader();

if (DR1.Read())
{
    Name.Text = DR1.GetValue(0).ToString();
}
4

4 回答 4

1
while (DR1.Read())
{

    if(DR1.GetName() == "YourSQLColumnName")
    {
      YourTextBox.Text = (string) DR1["YourSQLColumnName"];
    }

    // Your Other textboxes and columns which you want to match should follow as this template
}
于 2013-07-03T18:01:25.207 回答
0
SqlCommand cmd  = new System.Data.SqlClient.SqlCommand(sql, _conn);
SqlDataReader rdr = cmd.ExecuteReader();
System.Data.DataTable tbl = new System.Data.DataTable("Results");
tbl.Load(rdr);

if (tbl.Rows.Count > 0)
   Name.Text = tbl.Rows[0]["column_name"].ToString();
于 2013-07-03T17:28:31.210 回答
0
string cs=System.Configuration.ConfigurationManager.ConnectionString["DBCS"].ConnectionString;
using(OracleConnection con=new OracleConnection(cs))
{
sql="select empname from Emp where empno='"+empno+"'";
OracleCommand cmd  = new System.Data.OracleClient.OracleCommand(sql,con);
con.Open();
OracleDataReader rdr = cmd.ExecuteReader();
if(rdr.Read())
{
EmpName.Text=Convert.ToString(rd["empname"]);
}
}
于 2014-01-01T08:02:34.200 回答
0

我假设您希望同时处理更多行和更多列。

  • 尝试指定列。它可以在没有的情况下工作,但如果你这样做,性能会更好。
  • 我假设您有一个名为 PersonalUser 的具有指定属性的类。
  • 将它按排序顺序排列也很好,所以我补充说


    public List<PersonalUser> FetchMyData()
    {

        SqlConnection Conn = new SqlConnection(@"Data Source=rex;Initial Catalog=PersonalDetails;Integrated Security=True");
        SqlCommand Comm1 = new SqlCommand("Select Name, Address, TelephoneNo,Date From PersonalUsers order by Name", Conn);

        Conn.Open();
        SqlDataReader DR1 = Comm1.ExecuteReader();

        var result = new List<PersonalUser>();
        while (DR1.Read())
        {
            result.Add(new PersonalUser {
                Name = DR1.GetString(0);
                Address= DR1.GetString(1);
                TelephoneNo = DR1.GetString(2);
                Date = DR1.GetString(3)
              }
           );
        }


        return result;
    }

如果需求变得比这复杂得多,我也会考虑使用实体框架。

于 2014-01-01T08:28:44.800 回答