Tag: sql

超时exception导致SqlDataReader关闭?

我正在尝试从数据库中提取一些二进制数据并将其写入pdf文件。 在大多数情况下,这是在游泳,但偶尔的数据行似乎抛出一个特定的错误 – 超时已过期。 操作完成之前经过的超时时间或服务器没有响应。 请记住,这只发生在少数行上,并且从不随机。 相同的行总是抛出exception。 我不确定为什么会抛出exception,但我可以跳过导致问题并继续前进的行。 然而,我的问题是,当我捕获exception然后尝试移动到下一行时,我遇到另一个exception – InvalidOperationException – 读取器关闭时无效尝试调用Read。 这是否意味着读者一旦遇到exception就会自动关闭? 如何在没有任何戏剧的情况下继续前进到下一行? while (sdrReader.Read()) // Second exception happens here { try { byte[] byteData = new Byte[(sdrReader.GetBytes(0, 0, null, 0, int.MaxValue))]; // first exception happens here sdrReader.GetBytes(0, 0, byteData, 0, byteData.Length); string strOutputFileName = sdrReader.GetInt32(1).ToString() + “.pdf”; msMemoryStreams = new MemoryStream(); msMemoryStreams.Write(byteData, […]

Entity Framework在哪里存储属性名称与它在SQL中选择的列之间的映射?

我正在尝试使用ObjectQuery的ToTraceString()在EF(4.3)上构建一些自定义扩展,以从LINQ代码生成原始SQL。 我注意到在某些情况下,SQL中列的名称与查询元素类型的属性名称匹配,在其他情况下,它们被命名为C1,C2等。这使我很难做任何事情使用生成的SQL。 有谁知道如何将这些列名称映射回属性名称(我只关心能够为平面元组执行此操作,如匿名类型,如果有帮助,我不需要任何分层)。 依赖于私人/内部reflection的解决方案是完全可以接受的。 我知道我可能需要在将来的EF版本中对其进行调整。 此外,我只使用MS SqlServer(2008+),如果它有帮助。

如何让Linq to SQL识别动态存储过程的结果集?

我正在使用Linq-to-SQL和SQL Server后端(当然)作为项目的ORM。 我需要从一个从动态创建的表返回的存储过程中获取结果集。 这是proc的样子: CREATE procedure [RetailAdmin].[TitleSearch] ( @isbn varchar(50), @author varchar(50), @title varchar(50)) as declare @L_isbn varchar(50) declare @l_author varchar(50) declare @l_title varchar(50) declare @sql nvarchar(4000) set @L_isbn = rtrim(ltrim(@isbn)) set @l_author = rtrim(ltrim(@author)) set @l_title = rtrim(ltrim(@title)) CREATE TABLE #mytemp( [storeid] int not NULL, [Author] [varchar](100) NULL, [Title] [varchar](400) NULL, [ISBN] [varchar](50) NULL, […]

在不知道sqlDbType的情况下将DBNull.Value与SqlParame一起使用?

我正在使用SqlParameter将空值传递给可以为空的各种列的表。 问题是如果没有sqlDbType,SqlParameter看起来默认为nvarchar。 如果实际的db类型是varbinary,则会出现问题。 我得到一个例外说法 不允许从数据类型nvarchar到varbinary(max)的隐式转换。 使用CONVERT函数运行此查询。 当我创建SqlParameter时,我所知道的只是参数的名称和对象。 如果对象为null,SqlParameter显然无法推断出正确使用的类型,那么有没有办法将SqlParameter与null值一起使用,而不必在创建sql参数时知道sqlDbType? 基本上将DBNull传递给数据库而不指定类型,让数据库处理它?

entity framework无法识别的唯一密钥

我有两个表, Reports和Visualizations 。 Reports有一个字段VisualizationID ,它通过外键指向Visualization的同名字段。 它还具有在该字段上声明的唯一键。 VisualizationID不可为空。 这意味着关系必须为0..1到1,因为每个 Reports记录必须具有与之关联的唯一的非空Visualizations记录。 entity framework不会这样看待它。 我收到以下错误: Error 113: Multiplicity is not valid in Role ‘Report’ in relationship ‘FK_Reports_Visualizations’. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *. 这有什么问题? 如何让EF识别正确的关系多样性?

清理SQL数据

Google提出了各种关于清理Web访问查询的讨论,但我找不到任何解决我关注的问题: 在ac#程序中清理用户输入数据。 这必须通过可逆转换来完成,而不是通过移除来完成。 作为问题的一个简单例子,我不想破坏爱尔兰名字。 什么是最好的方法,是否有任何库函数可以做到这一点?

sql异步查询问题

那么,为什么这不会成为回调函数呢? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace sqlAsyncTesting { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(@”Data Source = bigapple; Initial Catalog = master; Integrated […]

SQL:在VarBinary列上按顺序执行UPDATE .WRITE

我正在尝试创建一个小的测试应用程序,它读取FileStream的块并将其附加到SQL Server 2005 Express上的VarBinary(max)列。 一切正常 – 列应该按照它应该填充,但我的机器似乎仍然将所有内容缓冲到内存中,我只是看不清楚原因。 我正在使用以下代码(C#): using (IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString)) { connection.Open(); string id = Guid.NewGuid().ToString(); using (IDbCommand command = connection.CreateCommand()) { command.CommandText = “INSERT INTO [BLOB] ([Id],[Data]) VALUES (@p1,0x0)”; SqlParameter param = new SqlParameter(“@p1”, SqlDbType.VarChar); param.Value = id; command.Parameters.Add(param); command.ExecuteNonQuery(); } if (File.Exists(textBox1.Text)) { using (IDbCommand command = connection.CreateCommand()) { […]

SqlDataReader.Read和SqlDataReader.NextResult之间的区别

这两种方法的主要区别是什么? 在msdn网站上,它解释如下,但我不明白。 Read将SqlDataReader推进到下一条记录。 (重写DbDataReader.Read()。) NextResult在读取批处理Transact-SQL语句的结果时,将数据读取器NextResult到下一个结果。 (重写dbDataReader.NextResult()。)

ScriptingOptions sql smo不支持脚本数据

我正在使用c#代码生成sql数据库脚本。 以下代码适用于create table但是当我尝试使用scriptOptions.ScriptData = true; 它抛出以下exception。 Microsoft.SqlServer.Smo.dll中发生未处理的“Microsoft.SqlServer.Management.Smo.FailedOperationException”类型的exceptionexception 附加信息:此方法不支持脚本数据。 码 public static string ScriptDatabase(string dbConnectionString, string databaseName) { SqlConnection conn = new SqlConnection(dbConnectionString); ServerConnection serverConn = new ServerConnection(conn); var server = new Server(serverConn); var database = server.Databases[databaseName]; var scripter = new Scripter(server); // I tried this code also // scripter.Options.ScriptData = true; ScriptingOptions scriptOptions = new […]