这是我的' SQL'查询并且必须通过' UserId'作为'其中'的唯一标识符查询中的子句。
SELECT aspnet_Roles.RoleName, aspnet_Roles.Description
FROM aspnet_Roles INNER JOIN
aspnet_UsersInRoles ON aspnet_Roles.RoleId = aspnet_UsersInRoles.RoleId INNER JOIN
aspnet_Users ON aspnet_UsersInRoles.UserId = aspnet_Users.UserId
WHERE (aspnet_Users.UserId = @param)
我要传递参数的C#代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlTypes;
public partial class Forms_RolesMgt_RoleList_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DSRolesTableAdapters.aspnet_RoleUsersTableAdapter userRoleAdapter = new DSRolesTableAdapters.aspnet_RoleUsersTableAdapter();
DataTable dt = new DataTable();
Guid user = Guid.NewGuid();
user = Guid.Parse(Session["UserId"] as string);
Response.Write(user);
dt = userRoleAdapter.GetDataByUserId(user);//At this line i am passing the parameter and getting the error
UserRolesGrdView.DataSource = dt;
UserRolesGrdView.DataBind();
}
}
}
运行时出错:无法启用约束。一行或多行包含违反非null,唯一或外键约束的值。
按查询预览数据时.xsd文件出错:无法将参数值字符串转换为Guid
我哪里错了?
答案 0 :(得分:0)
在您的情况下,您应该使用存储过程
CREATE PROCEDURE [dbo].[Name]
(@param uniqueidentifier
)
AS
BEGIN
SELECT aspnet_Roles.RoleName, aspnet_Roles.Description
FROM aspnet_Roles INNER JOIN
aspnet_UsersInRoles ON aspnet_Roles.RoleId = aspnet_UsersInRoles.RoleId INNER JOIN
aspnet_Users ON aspnet_UsersInRoles.UserId = aspnet_Users.UserId
WHERE (aspnet_Users.UserId = @param)
END
并在GetDataByUserId()方法中为sp添加参数:
command.Parameters.Add("@param",SqlDbType.UniqueIdentifier).Value=userId;