Tag: sql

linq给实体生成了sql

我在ado.netentity framework中的实体中遇到了一些问题。 基本上我正在做的是这样的: var results = (from c in companies where c.Name.StartsWith(letter) select c); 并将其转换为SQL,如: WHERE (CAST(CHARINDEX(@p, [Extent1].[Name]) AS int)) = 1 这很好,但我的表有数百万的记录,所以这运行非常慢。 我需要它生成的是: WHERE Name LIKE @p + ‘%’ 我搜索高低,除了使用存储过程或使用实体sql之外,找不到任何解决方案… 有没有办法通过linq做到这一点? 可能通过某种方式将linq扩展到实体linq提供程序,或以某种方式拦截命令树或生成的查询?

使用“使用”时最后关闭SqlConnection

我想关闭最后的SqlConnection,因为使用并没有真正关闭它,连接池变满了。 但是我没有意识到什么是正确的方法,因为conn对象在finally部分中不再可用。 try { using (var conn = new SqlConnection(_dbconnstr)) { //… } } catch (Exception ex) { //… } finally { conn.Close //?!?!?!?!??? }

存储过程的架构

我有一个程序,我想阅读程序的架构。 要检索视图模式,我使用此处显示的查询。 同样的方式我想获得存储过程的模式。 怎么弄呢? Plz显示了一些语法。 public static DataTable SchemaReader(string tableName) { string sql = string.Format(“Select * from {0}”, tableName); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.CommandType = CommandType.Text; SqlDataReader reader = cmd.ExecuteReader(); DataTable schema = reader.GetSchemaTable(); reader.Close(); conn.Close(); return schema; } 如果有任何查询请问。请提前谢谢。

如何从gridview获取列名?

我想知道如何从gridview获取列名? 按其编号而非名称。 喜欢:姓名|年龄|生日:(所以姓名= 0,年龄= 1等…) 谢谢。

如何计算c#中sql表的行数?

如何计算c#中sql表的行数? 我需要从我的数据库中提取一些数据……

防止ASP.NET Web应用程序上的SQL注入

我是C#和ASP.NET的新手。 我正在使用VS2005 C#和SQL Server 2005,并且已经做了一些关于防止SQL注入的研究 我的服务器端 Web应用程序中有几个函数,我不确定它们是否需要输入validation。 1)从工具箱登录控件。 我已经直接从VS Toolbox实现了登录控件,我尝试使用RegularExpressionValidator作为我的登录工具,但它似乎不起作用。 Microsoft是否已对该工具进行内置validation? 2)将Excel文件表上传到SQL Server数据库。 我有一个function,允许用户将Excel文件表上传到数据库中。 一开始我不觉得有必要validation它,因为没有打开的SQL查询,但之后我问自己是否有可能用户在excel文件中输入SQL查询,这将导致上传期间的SQL注入。 以下是我的上传代码片段,如果需要validation,我们期待提供建议: string connStr = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + strUploadFileName + “;Extended Properties=Excel 8.0;”; using (OleDbConnection connection = new OleDbConnection(connStr)) { string selectStmt = string.Format(“Select [COLUMNS] FROM [userlist$]”); OleDbCommand command = new OleDbCommand(selectStmt, connection); connection.Open(); Console.WriteLine(“Connection Opened”); // Create DbDataReader to Data […]

通过C#中调用的存储过程将Bytearray插入SQL

首先,我尝试了一切,并且无法理解为什么它不能正确更新我的varbinary字段。 在1728字节中,只有字节数组中的最后一个字节保存到字段中… 我生成我的字节数组如下: public static byte[] StringToByteArray(String hex) { int NumberChars = hex.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i < NumberChars; i += 2) bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); return bytes; } 我也试过以下一个: public static byte[] ParseHex(string hex) { int offset = hex.StartsWith(“0x”) ? 2 : […]

INSERT与UPDATE

我有以下查询: SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“chestionar”].ConnectionString); SqlCommand cmd = new SqlCommand(“INSERT INTO Raspunsuri Values(@raspuns,@cnp,@data,’1′,@ip,@idsesiune)”, con); cmd.Parameters.AddWithValue(“@cnp”, Session[“sesiune_cnp”]); cmd.Parameters.AddWithValue(“@raspuns”, textbox1.Text); cmd.Parameters.AddWithValue(“@data”, DateTime.Now.ToLocalTime()); cmd.Parameters.AddWithValue(“@ip”,ip); cmd.Parameters.AddWithValue(“@idsesiune”, id_sesiune); try { con.Open(); cmd.ExecuteNonQuery(); Response.Redirect(“User2.aspx”); } catch (Exception ex) { Console.WriteLine(“Error:” + ex); } finally { con.Close(); } 我需要的是看表中是否有任何记录,如果有更新,否则插入它。我怎么能实现呢?

linq to nhibernate compareto不支持

我有linq到nhibernate查询: var listka = from i in FakturyZakupu.Queryable where String.Compare(i.REJESTRY.REJ_KOD,sbWartoscBetween1.ToString()) >= 0 && String.Compare(i.REJESTRY.REJ_KOD,sbWartoscBetween2.ToString()) <= 0 select i; lista = listka.ToList(); 它编译得非常好,但是如果我使用它,则抛出exception: NotSupportedException int32 CompareTo(System.String,System.String) 我怎样才能在两个值之间使用linq查询字符串值。 就像在SQL中一样:select * from table from a a和b之间的id?

如何使用SqlDataReader获取浮点值?

在我的数据库中,我将NextStatDistanceTime值作为float。 当“ float time = reader.GetFloat(0); ”行已超出时,它会给出错误 系统无效的转换exception 如何在此代码中从sql命令获取浮点值? 这是我的代码: using (SqlConnection conn = new SqlConnection(@””)) { float totaltime = 0; for (int i = startStationIndex; i < endStationIndex; i++) { SqlCommand command = new SqlCommand("SELECT NextStatDistanceTime FROM [MetroDatabase].[dbo].[MetroStation] WHERE StationIndex = " + i + "", conn); try { conn.Open(); command.ExecuteNonQuery(); using (SqlDataReader reader […]