你这代码写的实在没法改,你还是看看我的宿舍管理系统吧,看懂一个就行了。[C#] 纯文本查看 复制代码 using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace Dal
{
public class studentsDal
{
public static students[] QueryAll()
{
string sql = "select * from students;";
DataTable dt = SqlHelp.QueryBySql(sql);
students[] result = new students[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
students students = new students();
students.id = Int32.Parse(dr["id"].ToString());
students.name = dr["name"].ToString();
students.sex = dr["sex"].ToString();
students.code = dr["code"].ToString();
students.birthday = DateTime.Parse(dr["birthday"].ToString());
students.mobile = dr["mobile"].ToString();
students.nativep = dr["nativep"].ToString();
result[i] = students;
i++;
}
return result;
}
public static students[] Find(string key)
{
string sql = "select * from students where id like '{0}' or name like '{0}' or sex like '{0}' or code like '{0}' or birthday like '{0}' or mobile like '{0}' or natavep like '{0}'";
sql = string.Format(sql, key);
DataTable dt = SqlHelp.QueryBySql(sql);
students[] result = new students[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
students students = new students();
students.id = Int32.Parse(dr["id"].ToString());
students.name = dr["name"].ToString();
students.sex = dr["sex"].ToString();
students.code = dr["code"].ToString();
students.birthday = DateTime.Parse(dr["birthday"].ToString());
students.mobile = dr["mobile"].ToString();
students.nativep = dr["nativep"].ToString();
result[i] = students;
i++;
}
return result;
}
public static Boolean Update(students students)
{
String sql = "update students set name = '{0}', sex = '{1}', code = '{2}', birthday = '{3}', mobile = '{4}', nativep = '{5}' where id = '{6}'";
sql = String.Format(sql, students.id, students.name, students.sex, students.code, students.birthday, students.mobile, students.nativep);
Boolean result = SqlHelp.Excute(sql);
if (result == true)
{
return true;
}
else
{
return false;
}
//return result;
}
public static Boolean Insert(students students)
{
String sql = "insert into students(id, name, sex, code, birthday, mobile, nativep) values('{0}','{1}','{2}', '{3}', '{4}', '{5}', '{6}')";
sql = String.Format(sql, students.id, students.name, students.sex, students.code, students.birthday, students.mobile, students.nativep);
Boolean result = SqlHelp.Excute(sql);
if (result == true)
{
return true;
}
else
{
return false;
}
//return result;
}
public static Boolean Delete(string key)
{
string sql = "delete from students where id =" + key;
Boolean result = SqlHelp.Excute(sql);
if (result == true)
{
return true;
}
else
{
return false;
}
//return result;
}
}
}
代码毫无章法和套路
呵呵
id like '{0}' or name like '{0}' or sex like '{0}' or code like '{0}' or birthday like '{0}' or mobile like '{0}' or natavep like '{0}
那里用得着这么多啊,
你需要那个就写那个就是了,不用把所有的字段都加上
natavep 这个好像写错了吧。
你只写一个name like '{0}' 就行了吧
而且包含是这样写的
string sql = "select * from students where name like '%{0}%' ";
这样就有数据了,
其他的你参考这个修改下吧
|