这可能是我的第一个C#计划,所以我请你耐心等待。 我试图创建一个应用程序,通过遵循在线教程添加/删除/更新本地MS Sql数据库中的学生数据。
使用表注册调用数据库本地Windows身份验证: 主键 - UserId int UserName varchar(100) 密码varchar(20) 国家varchar(100) email varchar(20)
程序使用WCF服务并且它正确启动,但是当我尝试调用InsertUserDetails()时我不断收到错误。
错误文字: 参数化查询'(@ UserID int,@ UserName nvarchar(4000),@ Password nvarchar(4000),@'需要参数' @ UserName&#39 ;,这不是供给
Service1.cs的C#代码:
public class Service1 : IService1
{
public DataSet SelectUserDetails()
{
SqlConnection con = new SqlConnection(@"Server=Robert;Database=Trying;Trusted_Connection=True;");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from Registration", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
public void UpdateRegistrationTable(UserDetails userInfo)
{
SqlConnection con = new SqlConnection(@"Server=Robert;Database=Trying;Trusted_Connection=True;");
con.Open();
SqlCommand cmd = new SqlCommand("update Registration set UserName=@UserName,Password=@Password,Country=@Country, Email=@Email where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
cmd.Parameters.AddWithValue("@Password", userInfo.Password);
cmd.Parameters.AddWithValue("@Country", userInfo.Country);
cmd.Parameters.AddWithValue("@Email", userInfo.Email);
cmd.ExecuteNonQuery();
con.Close();
}
public bool DeleteUserDetails(UserDetails userInfo)
{
SqlConnection con = new SqlConnection(@"Server=Robert;Database=Trying;Trusted_Connection=True;");
con.Open();
SqlCommand cmd = new SqlCommand("delete from Registration where UserID=@UserID", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
cmd.ExecuteNonQuery();
con.Close();
return true;
}
public string InsertUserDetails(UserDetails userInfo)
{
string Message;
SqlConnection con = new SqlConnection(@"Server=Robert;Database=Trying;Trusted_Connection=True;");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Registration(UserID,UserName,Password,Country,Email) values(@UserID,@UserName,@Password,@Country,@Email)", con);
cmd.Parameters.AddWithValue("@UserID", userInfo.UserID);
cmd.Parameters.AddWithValue("@UserName", userInfo.UserName);
cmd.Parameters.AddWithValue("@Password", userInfo.Password);
cmd.Parameters.AddWithValue("@Country", userInfo.Country);
cmd.Parameters.AddWithValue("@Email", userInfo.Email);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
Message = userInfo.UserName + " Details inserted successfully";
}
else
{
Message = userInfo.UserName + " Details not inserted successfully";
}
con.Close();
return Message;
}
}
}
IService1.cs的C#代码
[ServiceContract]
public interface IService1
{
[OperationContract]
string InsertUserDetails(UserDetails userInfo);
[OperationContract]
DataSet SelectUserDetails();
[OperationContract]
bool DeleteUserDetails(UserDetails userInfo);
[OperationContract]
void UpdateRegistrationTable(UserDetails userInfo);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class UserDetails
{
int userid;
string username;
string password;
string country;
string email;
[DataMember]
public int UserID
{
get { return userid; }
set { userid = value; }
}
[DataMember]
public string UserName
{
get { return username; }
set { username = value; }
}
[DataMember]
public string Password
{
get { return password; }
set { password = value; }
}
[DataMember]
public string Country
{
get { return country; }
set { country = value; }
}
[DataMember]
public string Email
{
get { return email; }
set { email = value; }
}
}
}
答案 0 :(得分:2)
这意味着您的UserName
课程的UserDetails
属性似乎是null
。检查它是否有值。
缓解此类错误的一种方法是通过指定插入方法的UserDetails
来提前检查contract
实例的有效性。
public string InsertUserDetails(UserDetails userInfo)
{
if(null==userInfo)
throw new Exception("userInfo is null");
if(String.IsNullOrEmpty(userInfo.UserName))
throw new Exception("UserName is null or empty");
// and only after this check succeeds do the insert
}
如果要允许空值,则必须将.NET NULL
转换为ado.NET DBNull.Value
。
cmd.Parameters.AddWithValue("@UserName", userInfo.UserName ?? DBNull.Value);
另一种可能的解决方案是将SQL包装在过程中,并通过提供默认参数使参数可为空。
create procedure usp_InserProc @UserName varchar(32) = NULL
begin
... sql code
end