c# ado.net sqlparameter fails
本文关键字:fails sqlparameter net ado | 更新日期: 2023-09-27 18:29:07
我用本地化语言(波斯语)从c#传递sqlparameter,但没有检索行。数据库已经为persioan_100_ci_ai进行了整理,表正在整理Database_default
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
SqlDataReader dr = default(SqlDataReader);
dt.TableName = "temp";
try {
if (!(conn.State == ConnectionState.Closed))
conn.Close();
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
string qry = "Select * from users WHERE [Name]=@UserName AND [Pwd]=@Password";
cmd.commandtext = qry;
cmd.Parameters.Add("@UserName", SqlDbType.NVarChar, 50).Value = "ادمین";
cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50).Value = "ادمین";
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows) {
dt.Load(dr);
}
return dt;
} catch (Exception ex) {
return null;
} finally {
dt = null;
cmd.Connection = null;
cmd.Parameters.Clear();
cmd.Dispose();
}
它在SSMS 中工作
declare @UserName nvarchar(50) = 'ادمين'
declare @Password nvarchar(50)= 'ادمين'
select * from Users where [name]=@UserName and [Pwd] = @Password
当我在查询中嵌入变量而不是参数时,它甚至可以工作
SqlCommand cmd = new SqlCommand();
DataTable dt = new DataTable();
SqlDataReader dr = default(SqlDataReader);
string pLoginName = "ادمین";
string pPassword = "ادمین";
dt.TableName = "temp";
try {
if (!(conn.State == ConnectionState.Closed))
conn.Close();
if (conn.State == ConnectionState.Closed)
conn.Open();
cmd.Connection = conn;
string qry = "Select * from users WHERE [Name]='" + pLoginName + "' AND [Pwd]='" + pPassword + "'";
cmd.CommandText = qry;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows) {
dt.Load(dr);
}
return dt;
} catch (Exception ex) {
return null;
} finally {
dt = null;
cmd.Connection = null;
cmd.Parameters.Clear();
cmd.Dispose();
}
不知道我错在哪里了。请大家指出。
我没有任何问题,我将这两个值都添加到了我的测试数据库中。这是的样本代码
// Code in BO logic method
string email = "ادمین";
string password = "ادمین";
SqlCommand cmd = new SqlCommand(@"SELECT * FROM Register WHERE Email=@Email AND Deleted=0 AND Password=@Pass");
cmd.Parameters.AddWithValue(@"Email", email.Trim());
cmd.Parameters.AddWithValue(@"Pass", password.Trim());
DataSet dst = Varmebaronen.AppCode.DA.SqlManager.GetDataSet(cmd);
//DataAccess Methods !
public static DataSet GetDataSet(SqlCommand cmd)
{
return GetDataSet(cmd, "Table");
}
public static DataSet GetDataSet(SqlCommand cmd, string defaultTable)
{
SqlConnection conn = GetSqlConnection(cmd);
try
{
DataSet resultDst = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(resultDst, defaultTable);
}
return resultDst;
}
catch
{
throw;
}
finally
{
conn.Close();
}
}
DataSet
有一条记录,请尝试使用AddWithValue
。如果再次没有发生任何事情,则问题不在参数中!
p.S不要使用一个静态连接,应用程序池是您的好友!
试着将参数和值赋值分离,如下所示:
// Create the parameter objects as specific as possible.
cmd.Parameters.Add("@UserName", System.Data.SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@Password", System.Data.SqlDbType.NVarChar, 50);
// Add the parameter values. Validation should have already happened.
cmd.Parameters["@UserName"].Value = "ادمین";
cmd.Parameters["@Password"].Value = "ادمین";
尝试使用这个:
cmd.Parameters.Add(new SqlParameter("@Password", "ادمین"));
编辑:
让我们尝试不同的方式。如果你想重新编码。我将发布一个旧的大学项目的例子。这基本上是同一个概念。也许不是最好的方法,但它有效。。。
我在.aspx页面上使用了DataAdapter、DataSet和GridView控件。你标记了ASP.net,但我不确定你想用什么来显示数据。
string selectsql2 = "SELECT * FROM [dbo].Event_View WHERE (EventName LIKE '%' + @EventName + '%')";
SqlConnection connect2 = new SqlConnection(connectionstring2);
SqlCommand cmd = new SqlCommand(selectsql2, connect2);
SqlParameter pm = new SqlParameter("@EventName", txtEvents.Text);
cmd.Parameters.Add(pm);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds2 = new DataSet();
adapter.Fill(ds2);
gvEvents.DataSource = ds2;
gvEvents.DataBind();