SqlCommand.Connection 属性

本文详细介绍了 SqlCommand 的 Connection 属性,该属性用于获取或设置 SqlCommand 实例所使用的 SqlConnection。文章提供了 Visual Basic、C# 和 C++ 的示例代码,并解释了在事务过程中更改 Connection 属性可能引发的异常。

SqlCommand.Connection 属性

获取或设置 SqlCommand 的此实例使用的 SqlConnection

[Visual Basic]
Public Property Connection As SqlConnection
[C#]
public SqlConnection Connection {get; set;}
[C++]
public: __property SqlConnection* get_Connection();
public: __property void set_Connection(SqlConnection*);
[JScript]
public function get Connection() : SqlConnection;
public function set Connection(SqlConnection);
属性值

与数据源的连接。默认值为空引用(Visual Basic 中为 Nothing)。

异常
异常类型条件
InvalidOperationException在事务进行过程中更改了 Connection 属性。
备注

如果在事务进行过程中设置 Connection,而且 Transaction 属性不为空,则会生成 InvalidOperationException。如果 Transaction 属性不为空,而事务已经提交或回滚,则 Transaction 设置为空。

示例

[Visual Basic, C#, C++] 下面的示例将创建一个 SqlCommand 并设置它的一些属性。

[Visual Basic] 
Public Sub CreateMySqlCommand()
    Dim mySelectQuery As String = "SELECT * FROM Categories ORDER BY CategoryID"
    Dim myConnectString As String = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"
    Dim myCommand As New SqlCommand(mySelectQuery)
    myCommand.Connection = New SqlConnection(myConnectString)
    myCommand.CommandTimeout = 15
    myCommand.CommandType = CommandType.Text
End Sub 'CreateMySqlCommand

[C#] 
public void CreateMySqlCommand() 
 {
    string mySelectQuery = "SELECT * FROM Categories ORDER BY CategoryID";
    string myConnectString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    SqlCommand myCommand = new SqlCommand(mySelectQuery);
    myCommand.Connection = new SqlConnection(myConnectString);
    myCommand.CommandTimeout = 15;
    myCommand.CommandType = CommandType.Text;
 }

[C++] 
public:
void CreateMySqlCommand() 
 {
    String* mySelectQuery = S"SELECT * FROM Categories ORDER BY CategoryID";
    String* myConnectString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
    SqlCommand* myCommand = new SqlCommand(mySelectQuery);
    myCommand->Connection = new SqlConnection(myConnectString);
    myCommand->CommandTimeout = 15;
    myCommand->CommandType = CommandType::Text;
 }

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 语言筛选器

要求

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版

请参见

SqlCommand 类 | SqlCommand 成员 | System.Data.SqlClient 命名空间 | CommandText | CommandTimeout | CommandType

 
#region 新增异常信息 /// <summary> /// 新增异常信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToolStripButtonInsertInfo_Click(object sender, EventArgs e) { TrafficCenter.Instance.skin.SkinAllForm = false; string site = "";//站点信息 string equipment = "";//设备信息 string component = "";//元器件信息 string operatorInfo = "";//操作人信息 string isReplace = "";//是否进行了更换 string errorInfo = "";//故障信息 string reason = "";//故障原因 string solution = "";//对策 var sheet1 = SelectDeviceInfosheetStep1(); //如果选择了站点信息 if (sheet1 != null) { site = sheet1["站点信息"].Data.ToString(); var sheet2 = SelectDeviceInfosheetStep2(site); //如果选择了设备信息 if (sheet2 != null) { equipment = sheet2["设备名称"].Data.ToString(); var sheet3 = SelectDeviceInfosheetStep3(site, equipment); //如果选择了元器件信息 if (sheet3 != null) { component = sheet3["元器件信息"].Data.ToString(); var sheet4 = InsertErroeInfo(site, equipment, component); //如果点击完成了异常信息的录入 if (sheet4 != null) { operatorInfo = sheet4["操作人(请输入工号)"].Data.ToString(); isReplace = sheet4["是否进行了更换"].Data.ToString(); errorInfo = sheet4["故障信息"].Data.ToString(); reason = sheet4["故障原因"].Data.ToString(); solution = sheet4["对策"].Data.ToString(); using (SqlConnection conn = new SqlConnection(ConnectionString)) { conn.Open(); Console.WriteLine("连接成功!"); Thread.Sleep(500); using (SqlCommand sqlCommand = new SqlCommand("INSERT INTO EquipmentAbnormalCondition(Sites,Equipment,Components,Operator,IsReplacement,FaultInformation,Reasons,Countermeasures) VALUES(@Sites,@Equipment,@Components,@Operator,@IsReplace,@ErrorInfo,@Reason,@Solution)", conn)) { //添加参数 sqlCommand.Parameters.AddWithValue("@Sites", site); sqlCommand.Parameters.AddWithValue("@Equipment", equipment); sqlCommand.Parameters.AddWithValue("@Components", component); sqlCommand.Parameters.AddWithValue("@Operator", operatorInfo); sqlCommand.Parameters.AddWithValue("@IsReplace", isReplace); sqlCommand.Parameters.AddWithValue("@ErrorInfo", errorInfo); sqlCommand.Parameters.AddWithValue("@Reason", reason); sqlCommand.Parameters.AddWithValue("@Solution", solution); sqlCommand.ExecuteNonQuery(); } conn.Close(); Invoke(new Action(() => { MessageBox.Show("异常信息上传成功!"); })); } } } } } TrafficCenter.Instance.skin.SkinAllForm = true; } /// <summary> /// 异常数据上传时选择站点信息 /// </summary> /// <returns></returns> private DataEntrySheet SelectDeviceInfosheetStep1() { DataEntrySheet sheet = new DataEntrySheet(); using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); using (SqlCommand sqlCommand = new SqlCommand("SELECT DISTINCT Sites FROM DeviceInformation", connection)) { using (SqlDataReader reader = sqlCommand.ExecuteReader()) { List<string> stations = new List<string>(); while (reader.Read()) { stations.Add(reader["Sites"].ToString()); } sheet.AddComboBoxField(new ComboBoxField() { Title = "站点信息", Items = stations.ToArray()// 设置ComboBox的Items属性 }); } } connection.Close(); } var dialog = provider.CreateDataEntrySheetDialog(sheet, s => { s.Title = "故障信息维护"; // 是否可取消 s.Cancelable = true; // 确认按钮文本 s.ConfirmOption.Text = "下一步"; // 取消按钮文本 s.CancelOption.Text = "取消"; }); dialog.Show(); if (dialog.Result.IsCancel) { return null; } return sheet; } /// <summary> /// 异常数据上传时选择设备信息 /// </summary> /// <param name="site"></param> /// <returns></returns> private DataEntrySheet SelectDeviceInfosheetStep2(string site) { DataEntrySheet sheet = new DataEntrySheet(); using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); using (SqlCommand sqlCommand = new SqlCommand("SELECT DISTINCT Equipment FROM DeviceInformation WHERE Sites = @Site", connection)) { sqlCommand.Parameters.AddWithValue("@Site", site); using (SqlDataReader reader = sqlCommand.ExecuteReader()) { List<string> stations = new List<string>(); while (reader.Read()) { stations.Add(reader["Equipment"].ToString()); } //显示站点信息的文本框 sheet.AddTextField(new TextField() { Title = "站点名称", Data = site }); sheet.AddComboBoxField(new ComboBoxField() { Title = "设备名称", Items = stations.ToArray()// 设置ComboBox的Items属性 }); } } connection.Close(); } var dialog = provider.CreateDataEntrySheetDialog(sheet, s => { s.Title = "故障信息维护"; // 是否可取消 s.Cancelable = true; // 确认按钮文本 s.ConfirmOption.Text = "下一步"; // 取消按钮文本 s.CancelOption.Text = "取消"; }); dialog.Show(); if (dialog.Result.IsCancel) { return null; } return sheet; } /// <summary> /// 异常数据上传时选择元器件信息 /// </summary> /// <param name="site"></param> /// <param name="equipment"></param> /// <returns></returns> private DataEntrySheet SelectDeviceInfosheetStep3(string site, string equipment) { DataEntrySheet sheet = new DataEntrySheet(); using (SqlConnection connection = new SqlConnection(ConnectionString)) { connection.Open(); using (SqlCommand sqlCommand = new SqlCommand("SELECT DISTINCT Components FROM DeviceInformation WHERE Sites = @Site AND Equipment = @Equipment", connection)) { sqlCommand.Parameters.AddWithValue("@Site", site); sqlCommand.Parameters.AddWithValue("@Equipment", equipment); using (SqlDataReader reader = sqlCommand.ExecuteReader()) { List<string> stations = new List<string>(); while (reader.Read()) { stations.Add(reader["Components"].ToString()); } //显示站点信息的文本框 sheet.AddTextField(new TextField() { Title = "站点名称", Data = site }); //显示设备信息的文本框 sheet.AddTextField(new TextField() { Title = "设备名称", Data = equipment }); sheet.AddComboBoxField(new ComboBoxField() { Title = "元器件信息", Items = stations.ToArray()// 设置ComboBox的Items属性 }); } } connection.Close(); } var dialog = provider.CreateDataEntrySheetDialog(sheet, s => { s.Title = "故障信息维护"; // 是否可取消 s.Cancelable = true; // 确认按钮文本 s.ConfirmOption.Text = "下一步"; // 取消按钮文本 s.CancelOption.Text = "取消"; }); dialog.Show(); if (dialog.Result.IsCancel) { return null; } return sheet; } /// <summary> /// 输入异常信息 /// </summary> /// <param name="site"></param> /// <param name="equipment"></param> /// <param name="component"></param> /// <returns></returns> private DataEntrySheet InsertErroeInfo(string site, string equipment, string component) { DataEntrySheet sheet = new DataEntrySheet(); //显示站点信息的文本框 sheet.AddTextField(new TextField() { Title = "站点名称", Data = site }); //显示设备信息的文本框 sheet.AddTextField(new TextField() { Title = "设备名称", Data = equipment }); //显示元器件信息的文本框 sheet.AddTextField(new TextField() { Title = "元器件名称", Data = component }); sheet.AddTextField(new TextField() { Title = "操作人(请输入工号)" }); sheet.AddComboBoxField(new ComboBoxField() { Title = "是否进行了更换", Items = new string[] { "是", "否" } }); sheet.AddTextField(new TextField() { Title = "故障信息", IsMultiline = true }); sheet.AddTextField(new TextField() { Title = "故障原因", IsMultiline = true }); sheet.AddTextField(new TextField() { Title = "对策", IsMultiline = true }); var dialog = provider.CreateDataEntrySheetDialog(sheet, s => { s.Title = "故障信息维护"; // 是否可取消 s.Cancelable = true; // 确认按钮文本 s.ConfirmOption.Text = "上传"; // 取消按钮文本 s.CancelOption.Text = "取消"; //窗口大小 s.DialogSize = new Size(500, 600); }); dialog.Show(); if (dialog.Result.IsCancel) { return null; } return sheet; } #endregion 帮我将四个临时窗口合成一个,要求功能一致
06-07
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值