Tag: SQL服务器

知道从C#调用SQL Server时何时重试或失败?

我有一个C#应用程序,它从一个有点片状环境中托管的SQL Server中获取数据。 我无法解决环境问题,所以我需要尽可能优雅地处理它们。 为此,我想重新尝试基础设施故障导致的操作,例如网络故障,SQL服务器脱机,因为它们正在重新启动,查询超时等。同时,我不想要如果逻辑错误失败,则重试查询。 我只是希望那些将exception冒泡到客户端。 我的问题是:区分环境问题(丢失连接,超时)和其他类型的exception(即使环境稳定会发生逻辑错误之类的事情)的最佳方法是什么。 C#中有一个常用的模式来处理这样的事情吗? 例如,是否有一个属性我可以在SqlConnection对象上检查以检测失败的连接? 如果没有,解决这个问题的最佳方法是什么? 对于它的价值,我的代码并不特别: using (SqlConnection connection = new SqlConnection(myConnectionString)) using (SqlCommand command = connection.CreateCommand()) { command.CommandText = mySelectCommand; connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { // Do something with the returned data. } } }

如何将SQL参数中的字符串数组传递给SQL中的IN子句

我正在以复杂的方式做的逻辑。 我只需要在存储过程中执行此查询: select Sizes, SUM(Quantity) from tbl_SizeBreakup where (Brand=@brand) and (Combo in (‘1′,’2’)) … 组合我必须在C#中使用SQL参数传递 DataSet dt = new DataSet(); cmd = new SqlCommand(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandText = “sp_Accessories”; cmd.Connection = con; cmd.Parameters.AddRange( new SqlParameter[] { new SqlParameter(“@Mode”,mode), new SqlParameter(“@Combo”,combo), }} 所以如果我传递1个参数,按预期工作。 我应该传递的组合是一个string[] (字符串数组)。 数组长度可以取决于用户在UI中选择的任何内容。 我的问题是,如何将string[]传递给new SqlParameter(“@Combo”,combo) ? 我的存储过程.. ALTER proc [dbo].[sp_Accessories] ( @Mode […]

从数据库加载PictureBox图像

我正在尝试将图像从数据库加载到PictureBox 。 我使用以下代码将它们加载到我的图片中。 我写了一些代码,但不知道我该怎么做才能继续。 任何帮助将不胜感激。 private void button1_Click(object sender, EventArgs e) { sql = new SqlConnection(@”Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True”); cmd = new SqlCommand(); cmd.Connection = sql; cmd.CommandText = (“select Image from Entry where EntryID =@EntryID”); cmd.Parameters.AddWithValue(“@EntryID”, Convert.ToInt32(textBox1.Text)); }

用于validationSQL脚本的代码

如何在使用.net 2.0和c#执行它们之前validationsql脚本? 如果sql无效,我想返回错误行。

如何从C#中的SQL Server数据库中检索数据?

我有一个数据库表,其中包含3列firstname , Lastname和age 。 在我的C#Windows应用程序中,我有3个名为textbox1文本框…我使用以下代码连接到我的SQL Server: SqlConnection con = new SqlConnection(“Data Source = .; Initial Catalog = domain; Integrated Security = True”); con.Open(); SqlCommand cmd = new SqlCommand(“Select * from tablename”, con); 我想从我的数据库中获取值; 如果我在textbox1给出一个值,它必须匹配数据库中的值并检索其他详细信息到相应的文本框。 我尝试过这种方法,但它不起作用: cmd.CommandText = “select * from tablename where firstname = ‘” + textBox1.Text + “‘ “; 我怎样才能检索文本框中的所有其他值?

SELECT sql语句的ExecuteNonQuery不返回任何行

如何检查ExecuteNonQuery for SELECT sql语句后返回没有行返回没有行?

必须声明标量变量@Id?

所以我试图从我的数据库中获取“客户”,但我得到了一个例外 System.Data.dll中出现“System.Data.SqlClient.SqlException”类型的exception,但未在用户代码中处理 附加信息:必须声明标量变量”@Id” 。 using Core; using System; using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DatabaseAccess { public class DbCustomer { private string ConnectionString = ConfigurationManager.ConnectionStrings[“local”].ConnectionString; private SqlConnection connection { get; set; } public DbCustomer() { connection = new SqlConnection(ConnectionString); } public Customer GetCustomer(int Id) { Customer customer = […]