假设学生信息表是student
增:public bool AddStu(string name,int age,string sex)
{
bool mark=false;
try
{
string connstr="链接字符串";
string sql="insert into student values("name","age","sex")";
SqlConnection conn=new SqlConnection(connstr);
conn.open();
SqlCommand cmd=new SqlCommand(conn);
cmd.CommandText=sql;
mark=true;
return mark;
}
catch
{
return mark;
}
finally
{
conn.close();
}
}
删:public bool DeleteStu(int id)
{
bool mark=false;
try
{
string connstr="链接字符串";
string sql="delete student where id ="+id;
SqlConnection conn=new SqlConnection(connstr);
conn.open();
SqlCommand cmd=new SqlCommand(conn);
cmd.CommandText=sql;
mark=true;
return mark;
}
catch
{
return mark;
}
finally
{
conn.close();
}
}
改:public bool AddStu(string name,int age,string sex,int id)
{
bool mark=false;
try
{
string connstr="链接字符串";
string sql="update student set name='+name+',age='+age+',sex='+sex+'where id='+id";
SqlConnection conn=new SqlConnection(connstr);
conn.open();
SqlCommand cmd=new SqlCommand(conn);
cmd.CommandText=sql;
mark=true;
return mark;
}
catch
{
return mark;
}
finally
{
conn.close();
}
}
查:public DataTable SelectStu(int id)
{
try
{
string connstr="链接字符串";
string sql="select * student where id ="+id;
SqlConnection conn=new SqlConnection(connstr);
conn.open();
SqlCommand cmd=new SqlCommand(conn);
cmd.CommandText=sql;
Datatable dt=cmd.ExecuteReader();
return dt;
}
catch
{
Console.ReadLine("没有找到数据!");
}
finally
{
conn.close();
}
}