Tag: sql

在sql db中插入一个包含特殊字符的字符串’

我想在sql表中插入一个可能包含’character的字符串。 我最好的方法是什么? 我应该在’之前插入\吗? 这是我在ac#代码中的命令: SqlCommand myCommand = new SqlCommand( String.Format( “insert into ACTIVE.dbo.Workspaces_WsToRefile values({0},'{1}’,getdate())”, folderId, NewWorkspaceName), myConnection); 其中NewWorkspaceName可能包含’字符,因此插入将导致exception。 感谢先进,哈达斯。

如何在SQL中检索给定StoredProcedure参数的.NET类型?

我正在SQL程序之上创建’通用’包装器,我可以解析所有必需参数的名称和sqltypes,但有没有办法如何获得它的’底层’.NET类型? 我的目标是做一些事情: SqlParameter param; object value; object correctParam = param.GetNETType().GetMethod(“Parse”, new Type[] { typeof(string) }).Invoke(value.ToString()); param.Value = correctParam; GetNETType是我需要的东西。 我知道它可以写成param.SqlDbType中的开关,但这是更短的方式,更短的注释代码意味着更低的维护:)

与C#数据库交互简介

到目前为止,在我的编程生涯中(两年)我没有太多的数据库经验,但我现在工作的公司广泛使用数据库作为他们的产品,我感觉落后于曲线。 所以我想知道如何最好地开始学习与C#的数据库交互。 我读过LINQ-to-SQL和ADO.net。 这些是正确的技术吗? 我从哪里开始? 编辑:感谢所有的回复。 有很多好的 – 我很难选择一个作为“答案”。 这对我帮助很大!

使用C#在SQL Server上执行sql脚本

SQL Server 2008 R2 Express,.Net Framework 4.0,Visual Studio 2010 我正在尝试从命令提示符应用程序执行SQL脚本。 我找到了一个样本 代码并试图实现相同的。 但是不承认以下使用声明。 using Microsoft.SqlServer.Management.Common; using Microsoft.SqlServer.Management.Smo; 我错过了任何assembly参考吗?

无法将参数日期的值从系统字符串转换为系统日期时间

public Kupac(SqlDataReader reader) { KupacId = Convert.ToInt32(reader[“KupacId”]); Ime = reader[“Ime”].ToString(); Prezime = reader[“Prezime”].ToString(); IdentifikacioniBroj = reader[“IdentifikacioniBroj”].ToString(); ClanOd = (DateTime)reader[“ClanOd”]; KorisnickoIme = reader[“KorisnickoIme”].ToString(); } public int KupacId { get; set; } public string Ime { get; set; } public string Prezime { get; set; } public string IdentifikacioniBroj { get; set; } public DateTime ClanOd { […]

如何解决system.data.sqlclient.sqlexception(0x80131904)错误

我已经使用C#和SQL Server 2008 R2在我的PC上创建了一个完美运行的应用程序。 我的连接字符串是: connectionString=”Data Source=KELVIN-PC;Initial Catalog=LMS;User ID=sa;Password=temperament” 但我已将其发布并安装在另一台PC上。 我还将LMS.bak文件复制到PC并在其上恢复了MS SQL Server 2008 R2上的数据库。 但是每当它必须连接到另一台机器上的数据库时,它会给出以下错误: System.Data.SqlClient.SqlException(0x80131904):在建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 validation实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供者:命名管道提供程序,错误:40 – 无法打开与SQL Server的连接)….. 我花了几个小时在互联网上搜索,但大多数回答的问题与我自己有一点点不同,我尝试的一些建议并没有真正起作用。

上传的Docx文件已损坏

我们的一个内部网站允许用户上传文档并将文件存储到SQL 2008数据库中。 我们遇到的问题是,当您尝试打开它们时,docx总是说已损坏。 单击确定后,他们然后继续打开罚款。 我做错了什么? (所有其他文件类型保存正常,包括.doc) 上传代码: // save the files string mimeType = Request.Files[i].ContentType; string filePath = Path.GetFileName(Request.Files[i].FileName); string fileName = Path.GetFileName(Request.Files[i].FileName); Stream fileStream = Request.Files[i].InputStream; long fileLength = Request.Files[i].InputStream.Length; if (fileLength > 0) { byte[] fileData = new byte[fileLength]; fileStream.Read(fileData, 0, (int)fileLength); _fileRepo.SaveFileToDatabase(_currentUser.Username, t.EncounterId, fileName, filePath, mimeType, fileData); } 将其插入数据库的代码 tblDocumentImage img = […]

为EnterpriseLibrary设置Sql CommandTimeout

我有一个SQL查询,执行时间超过30秒。 我知道我需要为命令对象设置CommandTimeout来克服这个问题。 但是,命令对象发生的第一个位置是企业库中的方法“LoadDataSet”。 我不认为我想在这里修改它。 有人可以告诉我一个适当的地方来设置它吗? 谢谢!

为什么ExecuteScalar返回NullReferenceException错误

我有以下函数检查列中是否存在用户( strU ),如果是,则返回1,否则返回0: public int AddDataScalar(string strU) { string strQueryExistence = “SELECT 1 FROM [OB].[h].[OP_PEONS] WHERE Executive= ‘” + strU + “‘”; int inNum; using (SqlConnection con = new SqlConnection(strConn)) { con.Open(); SqlCommand cmd = new SqlCommand(strQueryExistence, con); object value = cmd.ExecuteScalar().ToString(); if (value != null) { inNum = 1; } else { inNum = […]

使用其他格式插入数据库日期

我有一个gridview和sqldatasource。 我正在尝试在数据库中插入新记录,Date列类型为Date,默认格式为US,mm / dd / yyyy,我正在尝试插入另一种格式的日期。 SqlDataSource2.InsertCommand = “Insert into test (Name,Date) VALUES (@Name,@CONVERT(Date,’@Date’,104))”; SqlDataSource2.InsertParameters.Add(“Name”, nameText); SqlDataSource2.InsertParameters.Add(“Date”, DbType.DateTime, date.Text); 我得到:字符串未被识别为有效的DateTime。 谢谢