1

所以我有两种形式。一个是包含交付列表框的主表单,其中数据从数据库加载。另一种形式是交付表单,当用户从列表框中选择交付时,其详细信息应显示在交付表单中。到目前为止,我已经设法加载数据并设法在我的交付表单中显示一些文本。但是,我无法让我的交付表单显示列表框中所选交付的数据。

这是我的一些代码:

private void FrmDelivery_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data  Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\visits.mdf;Integrated  Security=True;User Instance=True");
con.Open();

SqlCommand cmd = new SqlCommand("SELECT CustomerName, CustomerAddress, ArrivalTime FROM   tblVisits", con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
this.txtCustomerName.Text = sdr["CustomerName"].ToString(); 
this.txtCustomerAddress.Text = sdr["CustomerAddress"].ToString();
this.txtArrivalTime.Text = sdr["ArrivalTime"].ToString();
}

sdr.Close();

if (theDelivery != null)
{
txtCustomerName.Text = theDelivery.customerName;
txtCustomerAddress.Text = theDelivery.customerAddress;
txtArrivalTime.Text = theDelivery.arrivalTime;
}
}
4

1 回答 1

0

我会用这种方式:

public string Get_Field(SqlConnection con, string Table,string Field,string where)
{

       DataSet RS_Temp = new DataSet();
       SqlDataAdapter DA;
       string SqlStr = "SELECT " &  Field &  " FROM " & Table;

       //Add an "If statment" if there is a where condition....

       DA = new OleDbDataAdapter(SqlStr, con);
       DA.Fill(RS_Temp, Table_Name);

            if (RS_Temp.Tables[Table_Name].Rows.Count == 0)
            {
                return ("");
            }
            if (System.Convert.IsDBNull(RS_Temp.Tables[Table_Name].Rows[0][0]))
            {
                return ("");
            }

            if (RS_Temp.Tables[Table_Name].Rows.Count != 0)
            {
                return (RS_Temp.Tables[Table_Name].Rows[0][0].ToString());
            }
            return ("");

}

当然不要忘记添加 Try & catch :

YOURTEXTBOX.Text = Get_Field(...)
于 2013-11-08T13:20:08.823 回答