Tag: sql

方法“ToString”没有重载需要1个参数

我有一个数据读取器来读取数据库中的数据。 我正在从销售表中阅读TotalPrice。 我想将总价格显示为2位小数。 代码是链接: TotalPrice.Text = myReader[“TotalPrice”].ToString(“N2”); 但是我输入了这个错误:方法“ToString”没有重载需要1个参数代码有什么问题?

数据库错误:位置0没有行

我相信这个问题是几个月前被问到的,但我相信我的情况有所不同,同样的规则也许不适用。 每次我执行此方法时都会弹出相同的错误。 位置0没有行。如果我将[0]改为[1]或[15]; [1]等处没有行。这是否意味着我的数据库连接都没有? 我应该写一些if语句来确定行是否在那里? public bool UpdateOrderToShipped(string order) { orderNumber = order; string batch = ConfigurationManager.AppSettings[“SuccessfulOrderBatch”]; string statement = “UPDATE SOP10100 SET BACHNUMB = ‘”+ batch +”‘ WHERE SOPNUMBE = @SOPNUMBE”; SqlCommand comm = new SqlCommand(statement, connectionPCI); comm.Parameters.Add(“SOPNUMBE”, orderNumber); try { comm.Connection.Open(); comm.ExecuteNonQuery(); comm.Connection.Close(); } catch(Exception e) { comm.Connection.Close(); KaplanFTP.errorMsg = “Database error: ” […]

在SQL 2005/2008中转义双引号

我有一家最近加入的国际公司,名为“BLA”BLAHBLAH“Ltd。(双引号是该名称的一部分。) 每当用户尝试搜索此公司时,通过输入“Blah或其他影响,搜索失败并在SQL Server中出现语法错误。 如何逃避这一点,以便搜索不会失败? 示例SQL: SELECT c.companyID, c.companyName, c.dateAdded, count(cm.maxID) as NumDirect FROM RussoundGeneral.dbo.Company c LEFT JOIN RussoundGeneral.dbo.CompanyMax cm ON (cm.companyId = c.companyId and cm.maxID is not null) WHERE CONTAINS ( companyName, ‘”BLAH*’ ) GROUP BY c.companyID, c.companyName, c.dateAdded ORDER BY c.companyName ASC

调用标量值函数时,ExecuteScalar始终返回null

为什么这会返回null? //seedDate is set to DateTime.Now; con is initialized and open. Not a problem with that using (SqlCommand command = new SqlCommand(“fn_last_business_date”, con)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.AddWithValue(“@seed_date”, seedDate);//@seed_date is the param name object res = command.ExecuteScalar(); //res is always null } 但是当我直接在DB中调用它时如下: select dbo.fn_last_business_date(‘8/3/2011 3:01:21 PM’) returns ‘2011-08-03 15:01:21.000’ 当我从代码中调用它时,我希望看到的结果 为什么,为什么,为什么?

如何识别C#中是否成功执行SQL作业

我有一个C#方法来执行SQL作业。 它成功执行SQL作业。 而且代码非常完美。 我正在使用标准SQL存储过程msdb.dbo.sp_start_job 。 这是我的代码.. public int ExcecuteNonquery() { var result = 0; using (var execJob =new SqlCommand()) { execJob.CommandType = CommandType.StoredProcedure; execJob.CommandText = “msdb.dbo.sp_start_job”; execJob.Parameters.AddWithValue(“@job_name”, “myjobname”); using (_sqlConnection) { if (_sqlConnection.State == ConnectionState.Closed) _sqlConnection.Open(); sqlCommand.Connection = _sqlConnection; result = sqlCommand.ExecuteNonQuery(); if (_sqlConnection.State == ConnectionState.Open) _sqlConnection.Close(); } } return result; } 这是在作业内执行的sp ALTER PROCEDURE […]

如何在带有参数的SQL查询中使用通配符

假设我有一个基本查询,如下所示: SELECT holiday_name FROM holiday WHERE holiday_name LIKE %Hallow% 这在我的SQL查询窗格中执行正常并返回’Halloween’。 当我尝试在我的代码中使用带有通配符’%’字符的参数时,会出现问题。 SqlConnection Connection = null; SqlCommand Command = null; string ConnectionString = ConfigurationManager.ConnectionStrings[“SQLdb”].ConnectionString; string CommandText = “SELECT holiday_name ” + “FROM holiday ” + “WHERE holiday_name LIKE %@name%”; Connection = new SqlConnection(ConnectionString); try { Connection.Open(); Command = new SqlCommand(CommandText, Connection); Command.Parameters.Add(new SqlParameter(“name”, HolidayTextBox.Text)); var results […]

关于UNION,INTERSECT和EXCEPT的SqlException

有人可以帮我解决这个例外吗? 我不明白它意味着什么或如何解决它…这是一个SqlException,带有以下消息: 使用UNION,INTERSECT或EXCEPT运算符组合的所有查询必须在其目标列表中具有相同数量的表达式。 我在伪代码中运行查询时得到它,如下所示: // Some filtering of data var query = data.Subjects .Where(has value) .Where(has other value among some set of values); // More filtering, where I need to have two different options var a = query .Where(some foreign key is null); var b = query .Where(some foreign key is not null) .Where(and that […]

使用Asp.Net C#选择所有图像

我是ASP.NET和C#的新手。 我试图从文件夹中检索所有图像并在页面上显示它,但它只选择一个图像。 我的ASP.NET代码: 我的C#代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Configuration; namespace Blog { public partial class index : System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“blogconnection”].ToString()); protected void Page_Load(object sender, EventArgs e) { con.Open(); string allimage; string qry=”select * from images”; SqlCommand cmd = […]

使用Visual C#将图像添加到SQL数据库

我正在开发一个用于图像处理的可视化C#程序。我正在尝试使用Visual C#(Windows窗体)和ADO.NET将图像添加到sql数据库。 我已经使用filestream方法将图像转换为二进制forms,但图像字节未保存在数据库中。 在数据库图像列中,它显示,并且没有数据被保存! 我已经尝试了很多插入方法(有和没有存储过程..等)但总是在数据库中得到相同的东西。 private void button6_Click(object sender, EventArgs e) { try { byte[] image = null; pictureBox2.ImageLocation = textBox1.Text; string filepath = textBox1.Text; FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); image = br.ReadBytes((int)fs.Length); string sql = ” INSERT INTO ImageTable(Image) VALUES(@Imgg)”; if (con.State != ConnectionState.Open) con.Open(); SqlCommand cmd […]

尝试将XML内容存储到SQL Server 2005失败(编码问题)

伙计们, 我有一个web服务,返回ISO-8859-1编码的数据 – 因为它不是我的,我无法改变:-( 出于审计目的,我想将这些调用生成的XML存储到SQL Server 2005表中,其中我有一个类型为“XML NULL”的字段。 从我的C#代码中,我尝试使用参数化查询将此XML内容存储到XML字段中,例如 SqlCommand _cmd = new SqlCommand(“INSERT INTO dbo.AuditTable(XmlField) VALUES(@XmlContents)”, _connection); _cmd.Parameters.Add(“@XmlContents”, SqlDbType.Xml); _cmd.Parameters[“@XmlContents”].Value = (my XML response); _cmd.ExecuteNonQuery(); 麻烦的是 – 当我运行此代码时,我收到一个错误: Msg 9402,Level 16,State 1,Line 1 XML解析:第1行,字符xy,无法切换编码 ?? 我试图弄清楚我可以在哪里以及如何“切换”编码 – 到目前为止没有运气。 这究竟意味着什么? 我无法在SQL Server 2005中使用ISO-8859-1编码存储XML? 或者有一个技巧:a)告诉SQL Server 2005只接受这种编码,或者b)在存储到SQL Server之前自动将webservice响应转换为UTF编码? 感谢任何提示,指示,提示! 渣