Tag: sql

在C#中调用SQL定义的函数

我在TSQL中编写了这个标量函数: create function TCupom (@cupom int) returns float as begin declare @Tcu float; select @Tcu = sum (total) from alteraca2 where pedido = @cupom if (@tcu is null) set @tcu = 0; return @tcu; end 我想在我的C#代码中调用此函数。 这是我到目前为止所拥有的: public void TotalCupom(int cupom) { float SAIDA; SqlDataAdapter da2 = new SqlDataAdapter(); if (conex1.State == ConnectionState.Closed) { conex1.Open(); […]

Linq To Sql仅比较时间

如何在不获取以下内容的情况下仅比较DateTime对象的时间 错误: An exception of type ‘System.NotSupportedException’ occurred in mscorlib.dll but was not handled in user code Additional information: The specified type member ‘TimeOfDay’ is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. 我的代码: var date = DateTime.Parse(query.DueTime); entities = entities.Where(r => r.DueTime.TimeOfDay.Equals(date.TimeOfDay));

SqlConnection.Close()里面使用语句

我正在使用此代码: public void InsertMember(Member member) { string INSERT = “INSERT INTO Members (Name, Surname, EntryDate) VALUES (@Name, @Surname, @EntryDate)”; using (sqlConnection = new SqlConnection(sqlConnectionString_WORK)) { sqlConnection.Open(); using (SqlCommand sqlCommand = new SqlCommand(INSERT, sqlConnection)) { sqlCommand.Parameters.Add(“@Name”, SqlDbType.VarChar).Value = member.Name; sqlCommand.Parameters.Add(“@Surname”, SqlDbType.VarChar).Value = member.Surname; sqlCommand.Parameters.Add(“@EntryDate”, SqlDbType.Date).Value = member.EntryDate; sqlCommand.ExecuteNonQuery(); } } } 如果我不添加sqlConnection.Close();是不是错误sqlConnection.Close(); 在处理之前? 我的意思是。 它没有显示任何错误,也没有任何问题。 首先关闭它会更好吗? […]

C#和SQL Server Management Studio中SQL查询的执行时间差异很大

我有一个简单的SQL查询,从C#运行时需要超过30秒,然后每次超时,而在SQL Server Management Studio上运行时立即成功完成。 在后一种情况下,查询执行计划不会显示任何麻烦,并且通过一些简单的操作可以很好地传播执行时间。 我在查询从C#运行时运行’ EXEC sp_who2 ‘,它被列为占用29,000毫秒的CPU时间,并且没有被任何东西阻止。 我不知道如何开始解决这个问题。 有没有人有一些见解? 查询是: SELECT c.lngId, … FROM tblCase c INNER JOIN tblCaseStatus s ON s.lngId = c.lngId INNER JOIN tblCaseStatusType t ON t.lngId = s.lngId INNER JOIN [Another Database]..tblCompany cm ON cm.lngId = cs.lngCompanyId WHERE t.lngId = 25 AND c.IsDeleted = 0 AND s.lngStatus = 1

从C#中的存储过程返回多个记录集

我不得不将ASP经典系统转换为C# 我有一个存储过程,最多可以返回7个记录集(取决于传入的参数)。 我需要知道如何简单地将所有记录集作为单独的DataTable返回,以便我可以遍历那里的任何内容,当我到达它的末尾时跳到下一个DataTable而不必运行多个SQL语句并使用多个适配器。填充语句以将每个表添加到DataSet中。 在经典中它是一个简单的Do While而不是objRS.EOF循环与objRS.NextRecordset()当我到达循环结束时移动到下一个语句。 有什么我可以使用,不需要完全重写当前的后端代码? 每个记录集具有不同数量的列和行。 他们彼此无关。 我们从Stored Proc’s返回多个记录集以减少流量。 例子很好。 谢谢

Cql批量复制/插入C#

我是JSON和SQLBulkCopy的新手。 我有一个JSON格式的POST数据,我希望使用C#在Microsoft SQL中批量复制/插入。 JSON格式: { “URLs”: [{ “url_name”: “Google”, “url_address”: “http://www.google.com/” }, { “url_name”: “Yahoo”, “url_address”: “http://www.yahoo.com/” }, { “url_name”: “FB”, “url_address”: “http://www.fb.com/” }, { “url_name”: “MegaSearches”, “url_address”: “http://www.megasearches.com/” }] } 类别: public class UrlData { public List URLs {get;set;} } public class Url { public string url_address {get;set;} public string url_name {get;set;} } 我怎样才能有效地做到这一点?

的ExecuteScalar(); 使用scope_identity()生成“System.InvalidCastException:指定的强制转换无效”

我有一个接受各种数据的表单(通过文本框和复选框列表),在click事件中,他们将所有数据插入表中并选择scope_identity,然后将其存储在变量中,以便在插入checkboxlist项时使用它使用循环到另一个表 根据许多答案和例子,这应该工作得很好!..但它给了我这个错误: Exception Details: System.InvalidCastException: Specified cast is not valid. Line 66: int NewBrandId = (int)comm.ExecuteScalar(); 这是我的linkbutton方法代码: protected void lnkbtnUploadAndSubmit_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“MOODbCenterConnection”].ConnectionString); SqlCommand comm = new SqlCommand(“INSERT INTO Brands (BrandName, BrandLogo, BrandWebsite, IsBrandVisible) VALUES (@Name, @Logo, @Website, @IsVisible); SELECT scope_identity();”, conn); comm.Parameters.Add(“@Name”, System.Data.SqlDbType.NVarChar, 50); comm.Parameters[“@Name”].Value = txtbxBrandName.Text; comm.Parameters.Add(“@Logo”, […]

在C#LINQ中实现RANK OVER SQL子句

我需要实现以下T-SQL子句…. RANK() OVER (PARTITION BY a.CategoryKey ORDER BY (x.Rate * @BASE_RATE ) DESC )as Rank …在C#LINQ中。 到目前为止,我想出的是…… var rank = data.GroupBy(d => d.CategoryKey) .Select(group => group.OrderByDescending(g => g.Rate * @BAES_RATE) 我想这会给我按照率* BASE_RATE排序的每个等级分区。 但我真正需要的是单行的单个等级 ,这是更大结果中的子查询。 所以我正在使用的完整SQL查询就像…. SELECT a.Rate, a.CategoryKey, a.ID, . . . RANK() OVER (PARTITION BY a.CategoryKey ORDER BY (x.Rate * @BASE_RATE ) DESC )as […]

将XSD转换为SQL关系表

是否有可用的东西可以帮助我将XSD转换为SQL关系表? XSD相当大(无论如何在我的世界里),如果有什么东西推动我前进而不是从头开始,我可以节省时间和无聊的打字。 如果你想看看XSD就在这里 。 它是交换MSDS的标准化/本地化格式。

如何在使用ExecuteNonQuery()时收到错误消息?

我正在以这种方式执行命令: var Command = new SqlCommand(cmdText, Connection, tr); Command.ExecuteNonQuery(); 在命令中有一个错误,但.NET不会抛出任何错误消息。 我怎么知道命令没有正确执行,以及如何获得exception?