dal代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data.SqlClient;
using System.Data;
namespace DAL
{
public class UsersDAL
{
public DataTable selectInfoUser(Users user)
{
string sql = string.Format("select*from users where name='{0}' and pwd='{1}'", user.name, user.pwd);
return DBHelper.ExecuteNonQuery(sql);
}
}
}
DBHelper代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
class DBHelper
{
public static string connstring = "Data Source=.;Initial Catalog=wangzhe;Integrated Security=True";
private static SqlConnection conn = null;
public static void InitConnection()
{
if (conn == null)
{
conn = new SqlConnection(connstring);
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
}
public static SqlDataReader GetDataReader(string sqlstr)
{
InitConnection();
SqlCommand cmd = new SqlCommand(sqlstr, conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
public static DataTable GetDataTable(string sqlstr)
{
InitConnection();
DataTable dt = new DataTable();
SqlDataAdapter dap = new SqlDataAdapter(sqlstr, conn);
dap.Fill(dt);
conn.Close();
return dt;
}
public static bool ExecuteNonQuery(string sqlstr)
{
InitConnection();
SqlCommand cmd = new SqlCommand(sqlstr, conn);
int result = cmd.ExecuteNonQuery();
conn.Close();
return result > 0;
}
}
}
datatable 需要通过sqldataadapter来fill
ExecuteNonQuery的返回值bool.你为什么会觉得他能在返回值类型为datatable的GetDataTable里renturn...
请问一下这应该怎么写
@小感伤3: 找一个返回值为datatable的方法..
谢谢大家,问题我已经解决了
不能用插入代码的形式么?