namespace DAL { public class xueshengbiaos { public xueshengbiaos xs(string username) { string sql = "select * from xueshengbiaos where usernames=@username"; List<SqlParameter> pra = new List<SqlParameter>(); pra.Add(new SqlParameter("@usernames", username)); DataTable dt= SqlHelper.ExecuteDataTable(sql, pra); } } }
SqlHelper部分
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace DAL { public class SqlHelper { private static readonly string connString = ConfigurationManager.ConnectionStrings["name"].ConnectionString; public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters ) { using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } } public static object ExecuteScalar(string sql, params SqlParameter[] parameters) { using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } } public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters) { DataSet set = new DataSet(); using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { if (parameters!=null) { cmd.Parameters.AddRange(parameters); } SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(set); return set.Tables[0]; } } } internal static void ExecuteDataTable(string sql, List<SqlParameter> pra) { throw new NotImplementedException(); } } }
return set.Tables[0]; 估计set或者set.Tables[0]是null,需要对应的地方检查null,否则在null上调用方法肯定异常
用SqlParameter[] 代替即可。
SqlParameter[] preate ={
new SqlParameter("@username",username)
};
用的是第二个无返回值的静态方法:internal static void ExecuteDataTable
internal static void ExecuteDataTable(string sql, List<SqlParameter> pra) { throw new NotImplementedException(); }
这个有返回值吗?
你调的是 void ExecuteDataTable(string sql, List<SqlParameter> pra) 这个方法,它确实返回void(就是每返回值),所以无法用 DataTable类型的变量来引用。