数据库创建在上一篇,这里不写了
using System;
using System.Data;using System.Data.SqlClient;
namespace sql1._4
{
class Program
{
static string constr = @"Data Source=(local);Initial Catalog=MY_DBMS;Integrated security=false;User=sa;PWD=111aaa";
static void Main(string[] args)
{
using(SqlConnection con=new SqlConnection(constr))
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from student where sno=@xuehao", con);
cmd.Parameters.Add("@xuehao", SqlDbType.Char).Value = "2529";
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
string sno = reader.GetString(0);
string name = reader.GetString(1);
Console.WriteLine(sno + " " + name);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}
}