Tag: sql server 2008 r2

按entity framework创建记录时未插入数据库默认值

我在sqlserver 2008中有一个LoginRecord表,其中包含以下列结构 – LoginId – int, identity UserId – int LoginDateTime- Allow nulls false,default value getdate() 我按entity framework6插入新纪录如下─ db.LoginRecords.Add(new LoginRecord() { UserId = UserId }); db.SaveChanges(); 但是在LoginDateTime表中,正在插入空值。 它应该是当前的日期时间。 我正在使用数据库第一种方法。 怎么能克服这个问题?

Code First无法启用迁移

我正在尝试启用迁移,但它正在抛出exception: 检查上下文是否以现有数据库为目标… System.TypeInitializationException:’System.Data.Entity.Migrations.DbMigrationsConfiguration`1’的类型初始值设定项引发exception。 —> System.TypeInitializationException:’System.Data.Entity.Internal.AppConfig’的类型初始值设定项引发了exception。 —> System.Configuration.ConfigurationErrorsException:配置系统初始化失败—> System.Configuration.ConfigurationErrorsException:必须在’section’标签上指定’name’属性。 我假设App.config文件没有正确设置(当我添加EF包时它自动设置)。 我所做的就是添加连接字符串: 我正在使用SQL Server 2008 R2。 因为我有一个连接字符串,我不相信我需要defaultconnectionfactory。 我对么? (注意:即使没有本节,我仍然会得到相同的例外) 我还缺少什么?

.Skip()。entity framework上的Take()导航属性正在我的SQL Server上执行SELECT *

我在生成的部分类上有一个方法,如下所示: var pChildren = this.Children .Skip(skipRelated) .Take(takeRelated) .ToList(); 当我查看我的SQL Server时,我可以看到生成的代码正在执行SELECT *.* FROM Children此代码直接从我的类中获取,我已经validation了我的Skip / Take的顺序是在我的.ToList之前。 如果我删除.ToList,那行很快(并且没有SQL发送到我的数据库),但是当我尝试对结果进行foreach ,我得到了发送到我的DB的相同SQL: SELECT *.* FROM Children 。 使用.Skip和。在我的实体的导航属性上有什么特别的东西需要做吗? 更新 我将尝试获取实际生成的SQL,我目前没有为此设置。 我找到了第一个,因为它出现在SSMS的“最近的昂贵查询”列表中。 运行这个: var pChildren = this.Children //.Skip(skipRelated) //.Take(takeRelated) .ToList(); 返回〜4,000,000行,需要约25秒。 运行这个: var pChildren = this.Children //.Skip(skipRelated) .Take(takeRelated) .ToList(); 返回〜4,000,000行,需要约25秒。 正如我所说,我将抓住为这些生成的SQL并将它们构建起来。

过程或函数需要参数’未提供

你好朋友我面临一个问题,将数据加载到网格视图。 页面加载事件调用像loaddata()里面的一个方法我写这个代码 using (SqlConnection Sqlcon = new SqlConnection(strCon)) { using (SqlCommand cmd = new SqlCommand()) { Sqlcon.Open(); cmd.Connection = Sqlcon; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = “SP_Marketing”; //cmd.Parameters.Add(new SqlParameter(“@Sno”, (object) ?? null.Value)); cmd.Parameters.Add(new SqlParameter(“@pvchAction”, SqlDbType.VarChar,50)); cmd.Parameters[“@pvchAction”].Value = “select”; cmd.Parameters.Add(“@pIntErrDescOut”, SqlDbType.Int).Direction = ParameterDirection.Output; SqlAda = new SqlDataAdapter(cmd); ds = new DataSet(); SqlAda.Fill(ds); GridViewSample.DataSource = ds; GridViewSample.DataBind(); } […]

退出应用程序时,.mdf文件中的数据丢失

最近我在SQL Server 2008 R2数据库上工作。 我创建数据库并在我的应用程序中附加相同数据库的.mdf文件,其中包含一些默认数据。 正确运行应用程序默认数据。 现在我插入,更新我的应用程序中的一些数据,它的工作正常。 但是当我退出应用程序并再次运行应用程序时,最后添加和更新的数据会丢失,但默认数据会像以前一样正常。 请帮忙。 为什么新的

我尝试使用Windows Universal PlatformApp连接到SQL Server 2008R2时出现System.Data.SqlClient.SqlException

嗨,当我尝试从Windows通用平台应用程序使用System.Data.SqlClient连接到Microsoft SQL Server 2008R2时,我得到以下exception: System.Data.SqlClient.SqlException:’已成功与服务器建立连接,但在登录过程中发生错误。 (提供者:TCP提供者,错误:0 – 操作成功完成)’ 这是我的连接字符串,我正在使用SQL Authentification: string connectionString = “{Data Source=xxx.xxx.xxx.xxx; Initial Catalog=TheDatabase;Integrated Security=false;User ID=user;Password=Password}” 当我尝试连接到2016 SQL Server的作品时,这是我的代码: using (SqlConnection connection= new SqlConnection(connectionString)) { connection.Open(); } 在connection.Open()上抛出了exception 需要考虑的事项: 我已经在Package.appxmanifest(enterpriseAuthentication,privateNetworkClientServer)中设置了所有必需的预留 SQL Server具有Windows和SQL身份validation,并启用了TCP 该软件包的目标版本是Windows 10 Fall Creators Update(10.0; Bulid 16299) 我已经尝试过Integrated Security = true,exception是一样的 其他应用程序(非WUP)在2008R2服务器上运行良好,也是带.NET的C#应用​​程序 这个主题解释了同样的问题: 链接到问题 SQL Server 2008R2的Service Pack 3可以提供帮助吗? 是否有解决方案或更好的方法使2008R2与WUP协同工作?

如何在defaultprocedure中将默认值传递给日期参数 – Sqlserver 2008?

这是我的存储过程: ALTER proc [dbo].[allrecp] @peid int=0, @pename varchar(20)=”, @pdes varchar(20)=”, @pdoj date=’1111-1-1′, @sal money=0, @dept int=0, @loc int=0,@i int=0 as begin if(@i=1) begin insert into Employee values(@pename,@pdes,@pdoj,@sal,@dept,@loc) end if(@i=2) begin update Employee set ENAME=@pename,DESEGNATION=@pdes,DOJ=@pdoj,SALARY=@sal,DEPTID=@dept,LOCATIONID=@loc WHERE EMPID=@peid end if(@i=3) delete EMPLOYEE where EMPID=@peid end 我的c#代码是: private void btndelete_Click(object sender, EventArgs e) { parameteres(3); } private void […]

什么是在C#中打开/关闭SqlConnection的最佳方法

我想知道用Sql Server 2008R2 Express Edition数据库打开SqlConnection最佳方法是什么。 此版本的Sql具有RAM使用率和CPU使用率的限制,因此我们必须采用最佳方法来打开SqlConnection 。 现在我正在检查每个方法的开始和结束时的连接。 这是一个例子。 private void CheckValidId(string Id) { CheckConnectionStatus(); try { sqlConnection.Open(); sqlCommand = new SqlCommand(“select * from ValidId where id=@id”, sqlConnection); sqlCommand.Parameters.AddWithValue(“@id”, Id); sqlDataReader = sqlCommand.ExecuteReader(); While (sqlDataReader.Read()) { string Test = sqlDataReader[“Id”].toString(); MessageBox.Show(“Value of Id : ” , Test); } } catch (Exception exp) { MessageBox.Show(exp.Message.ToString(), “Exception […]

从两个逗号分隔的参数更新表作为输入

我在前端有一个Gridview,其中Grid有两列:ID和Order,如下所示: ID Order 1 1 2 2 3 3 4 4 现在用户可以像前端Gridview一样更新订单: ID Order 1 2 2 4 3 1 4 3 现在,如果用户单击“保存”按钮,则ID和订单数据将作为@sID =(1,2,3,4)和@sOrder =(2,4,1,3)发送到存储过程 现在,如果我想更新订单并进行保存,我想将其存储到数据库中。 通过存储过程如何更新到表中以便更新表,而选择它时会给我以下结果: ID Order 1 2 2 4 3 1 4 3

无法将DBNull.Value强制转换为“System.DateTime”。 请使用可空类型

这是我的代码 var finalResults = (from r in results.AsEnumerable() where r.Field(“PrintTime”) is DBNull 其中PrintTime是我的Sql Server 2008 r2数据库中的一列,其类型是datetime并且可以为空 我有这个例外: 无法将DBNull.Value强制转换为“System.DateTime”。 请使用可空类型。 你能帮帮忙吗?