string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders"; SqlConnection myConnection = new SqlConnection(myConnString); SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection); myConnection.Open(); SqlDataReader myReader; myReader = myCommand.ExecuteReader(); // Always call Read before accessing data. if(myReader.Read()) { textbox1.Text= myReader.GetString(1);//赋值 } // always call Close when done reading. myReader.Close(); // Close the connection when done with it. myConnection.Close();
SqlConnection conn = new SqlConnection("server=.;database=northwind;uid=sa;pwd="); SqlCommand cmd = new SqlCommand("select title from employees where employeeid=1", conn); conn.Open(); TextBox1.Text = (string)cmd.ExecuteScalar();//读取返回记录的首行首列 conn.Close();
SqlConnection myConnection = new SqlConnection(myConnString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
// Always call Read before accessing data.
if(myReader.Read())
{
textbox1.Text= myReader.GetString(1);//赋值
}
// always call Close when done reading.
myReader.Close();
// Close the connection when done with it.
myConnection.Close();
{
this.TextBox.Text=dr["姓名"].ToString();//
}
SqlCommand cmd = new SqlCommand("select title from employees where employeeid=1", conn);
conn.Open();
TextBox1.Text = (string)cmd.ExecuteScalar();//读取返回记录的首行首列
conn.Close();