private void button2_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=XX;Initial Catalog=DBData;User ID=sa;Password=111111"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "insert [DBData].[dbo].[T_Student] (Number,Name,Score) output inserted.ID values('012345','LiLei','89')";
long id = (long)cmd.ExecuteScalar(); //ExecuteScalar():执行查询,返回结果集中的第一行的第 一列
MessageBox.Show("操作成功: i=" + id.ToString());
}
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=XX;Initial Catalog=DBData;User ID=sa;Password=111111"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Select * from [DBData].[dbo].[T_Student] where score<160";
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
MessageBox.Show(reader.GetString()); //获取指定列的字符串的值
}
}
}
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection conn = new SqlConnection("Data Source=XX;Initial Catalog=DBData;User ID=sa;Password=111111"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from [DBData].[dbo].[T_Student] where Score<@Score";
cmd.Parameters.Add(new SqlParameter("@Score", ));
DataSet dataset = new DataSet();
//SqlDataAdapter是一个帮我们把SqlCommand查询结果填充到DataSet中的类
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dataset);
DataTable dt = dataset.Tables[];
}
}
MessageBox.Show("操作成功");
}