EF 参数化 SqlParameter 字符串 为 null 会报错
string sql = "declare @CurrentId int; " +
"INSERT INTO [dbo].[Customer] ([Name], [PhoneNo], [CreateUserId], [CreateTime], [AddSource] , [PartnerId], [Deleted],[UpdateTime]) " +
"VALUES (@Name, @PhoneNo, @CreateUserId, @CreateTime, @AddSource ,@PartnerId, 0,@UpdateTime); " +
"set @CurrentId=SCOPE_IDENTITY(); " +
"INSERT INTO [dbo].[CustomerDetail] ([CustomerId], [Sex], [WeChatNo], [QQ], [Email] , [CompanyName], [CompanyAddress], [CompanyWebSite],[Level]) " +
"VALUES (@CurrentId, @Sex, @WeChatNo, @QQ, @Email , @CompanyName, @CompanyAddress,@CompanyWebSite,@Level);" +
" INSERT INTO [dbo].[UserCustomer] ([SysUserId] ,[CustomerId] ,[NoteName] ,[Remake] ,[Deleted],[UpdateTime]) " +
"VALUES (@SysUserId ,@CurrentId ,@NoteName ,@Remake ,0,@ucUpdateTime);";
List args = new List {
new SqlParameter ("Name", request.BaseInfo.Name ) ,
new SqlParameter ( "PhoneNo", request.BaseInfo.PhoneNo ),
new SqlParameter ( "CreateUserId", this.Token_UserId ),
new SqlParameter ( "CreateTime", DateTime.Now ),
new SqlParameter ( "AddSource", (int)AddSource.Mobile ),
new SqlParameter ( "PartnerId", this.Token_PartnerId),
new SqlParameter ( "Sex", request.DetailInfo.Sex ?? (int)Sex.未知),
new SqlParameter ( "WeChatNo", request.DetailInfo.WeChatNo ),
new SqlParameter ( "QQ", request.DetailInfo.QQ ),
new SqlParameter ( "Email", request.DetailInfo.Email ),
new SqlParameter ( "CompanyName", request.DetailInfo.CompanyName),
new SqlParameter ( "CompanyAddress", request.DetailInfo.CompanyAddress ),
new SqlParameter ( "CompanyWebSite", request.DetailInfo.CompanyWebSite ),
new SqlParameter ( "Level", request.DetailInfo.Level??(int)CustomerLevelEnum.Default),
new SqlParameter ("SysUserId", this.Token_UserId ) ,
new SqlParameter ( "NoteName", request.UserCustomerInfo.NoteName ),
new SqlParameter ( "Remake", request.UserCustomerInfo.Remark ),//这里如果字符串为null 会报错
new SqlParameter ( "UpdateTime", DateTime.Now ),
new SqlParameter ( "ucUpdateTime", DateTime.Now ),
};
await DbCtx.Database.ExecuteSqlCommandAsync(sql, args.ToArray());
Dapper 的不会
string sql = "declare @CurrentId int; " +
"INSERT INTO [dbo].[Customer] ([Name], [PhoneNo], [CreateUserId], [CreateTime], [AddSource] , [PartnerId], [Deleted],[UpdateTime]) " +
"VALUES (@Name, @PhoneNo, @CreateUserId, @CreateTime, @AddSource ,@PartnerId, 0,@UpdateTime); " +
"set @CurrentId=SCOPE_IDENTITY(); " +
"INSERT INTO [dbo].[CustomerDetail] ([CustomerId], [Sex], [WeChatNo], [QQ], [Email] , [CompanyName], [CompanyAddress], [CompanyWebSite],[Level]) " +
"VALUES (@CurrentId, @Sex, @WeChatNo, @QQ, @Email , @CompanyName, @CompanyAddress,@CompanyWebSite,@Level);" +
" INSERT INTO [dbo].[UserCustomer] ([SysUserId] ,[CustomerId] ,[NoteName] ,[Remake] ,[Deleted],[UpdateTime]) " +
"VALUES (@SysUserId ,@CurrentId ,@NoteName ,@Remake ,0,@ucUpdateTime);";
DynamicParameters parms = new DynamicParameters();
parms.Add("Name", request.BaseInfo.Name);
parms.Add("PhoneNo", request.BaseInfo.PhoneNo);
parms.Add("CreateUserId", this.Token_UserId);
parms.Add("CreateTime", DateTime.Now);
parms.Add("AddSource", (int)AddSource.Mobile);
parms.Add("PartnerId", this.Token_PartnerId);
parms.Add("Sex", request.DetailInfo.Sex ?? (int)Sex.未知);
parms.Add("WeChatNo", request.DetailInfo.WeChatNo);
parms.Add("QQ", request.DetailInfo.QQ);
parms.Add("Email", request.DetailInfo.Email);
parms.Add("CompanyName", request.DetailInfo.CompanyName);
parms.Add("CompanyAddress", request.DetailInfo.CompanyAddress);
parms.Add("CompanyWebSite", request.DetailInfo.CompanyWebSite);
parms.Add("Level", request.DetailInfo.Level ?? (int)CustomerLevelEnum.Default);
parms.Add("SysUserId", this.Token_UserId);
parms.Add("NoteName", request.UserCustomerInfo.NoteName);
parms.Add("Remake", request.UserCustomerInfo.Remark);
parms.Add("UpdateTime", DateTime.Now);
parms.Add("ucUpdateTime", DateTime.Now);
//执行sql
await this.DbConn.ExecuteAsync(sql, parms);