有一个相关的问题:
但我想知道差异是什么,以及不同方式是否存在任何问题.
我通常使用这样的结构:
using (SqlConnection conn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(SQL, conn))
{
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = Settings.Default.reportTimeout;
cmd.Parameters.Add("type", SqlDbType.VarChar, 4).Value = type;
cmd.Connection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(ds);
}
//use data
}
Run Code Online (Sandbox Code Playgroud)
现在有几种方法可以添加cmd参数,我想知道哪个是最好的:
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = "Bob";
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = "Bob";
cmd.Parameters.Add("@Name").Value = "Bob";
cmd.Parameters.AddWithValue("@Name", "Bob");
Run Code Online (Sandbox Code Playgroud)
在我假设的变量传递中具有字段的长度是不可取的,因为它是可以在数据库中稍后更改的魔术值.它是否正确?它是否会导致以这种方式(性能或其他)传递varchar的任何问题,我假设它默认为varchar(max)或数据库等效.我很高兴这会奏效.
更关心我的部分是如果我使用上面列出的第三或第四个选项,则丢失SqlDbType枚举我根本不提供类型.有没有这种情况不起作用的情况我可以想象varchar被错误地转换为char或者反之亦然的问题,或者可能是带有小数的问题....
就数据库而言,我认为字段类型的变化远不如长度,因此值得保留?
我试图将表值参数传递给存储过程,但我不断得到一个异常(见下文).
SqlCommand c = new SqlCommand("getPermittedUsers", myConn) { CommandType = CommandType.StoredProcedure };
c.Parameters.AddWithValue("@intNotifyingUserId", notifyingUserId);
c.Parameters.AddWithValue("@tSelectedPdfIds", sharedPdfs).SqlDbType = SqlDbType.Structured;
SqlDataReader dr = c.ExecuteReader();Run Code Online (Sandbox Code Playgroud)
类型在服务器上定义如下:
CREATE TYPE [dbo].[IdList] AS TABLE(
[Id] [int] NOT NULL
)Run Code Online (Sandbox Code Playgroud)
我试图通过sharedPdfs为List<int>,和IQueryable<int>,但不断收到以下异常:
Object must implement IConvertible.
谁知道我做错了什么?文档暗示我应该能够将列表作为TVP传递但不提供任何示例.
谢谢.
我在 SQL Server 中有一个用户定义的函数,它接受 TVP(表值参数)作为参数。在 EF 中,如何从 C# 调用这样的函数?
我尝试使用该方法,ObjectContext.CreateQuery<>但出现以下错误:
函数“QueryByParam”的参数“param”无效。参数只能是可以转换为 Edm 标量类型的类型。
也尝试过方法ObjectContext.ExecuteStoreQuery<>并得到相同的错误。IQueryable无论如何它都不会返回。
示例代码
[DbFunction(nameof(SampleDbContext), "QueryByParam")]
public IQueryable<SecurityQueryRow> QueryByParam(IEnumerable<ProfileType> profiles, bool isActive = false)
{
DataTable dataTable = ....
ObjectParameter profilesParam = new ObjectParameter("profileTypeIds", dataTable);
ObjectParameter isActiveParam = new ObjectParameter("isActive ", isActive);
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<SecurityQueryRow>(
string.Format("[{0}].[{1}](@profileTypeIds, @isActive)", GetType().Name, "QueryByParam"),
profilesParam,
isActiveParam);
}
Run Code Online (Sandbox Code Playgroud)
要求是我们需要一个 IQueryable 返回,而不是消耗的结果。
c# sql-server entity-framework table-valued-parameters entity-framework-6
我有一个接受表值参数的存储过程。我想将 int 数组传递给它,但现在我收到此错误:
无法将参数值从 Object[] 转换为 IEnumerable`1。
我的代码:
int[] orderIds; //int array which contains 0 or more values
List<SqlParameter> queryList = new List<SqlParameter>
{
new SqlParameter("@companyId", SqlDbType.Int) {Value = companyId},
new SqlParameter("@marketplaceId", SqlDbType.Int) {Value = marketplaceId},
new SqlParameter("@marketlocalId", SqlDbType.Int) {Value = marketLocal},
new SqlParameter("@storeId", SqlDbType.Int) {Value = storeId}
};
if (orderIds != null)
{
queryList.Add(new SqlParameter("@OrderIdsSpecified", SqlDbType.Bit) {Value = 1});
// converting int array to object array
queryList.Add(new SqlParameter("@OrderIds", SqlDbType.Structured) {Value = orderIds.Select(orderId => (object)orderId).ToArray() });
}
using (SqlDataReader reader …Run Code Online (Sandbox Code Playgroud)