SqlCommand.Parameters.add()

本文详细介绍了SQL语句中参数的添加方法,包括参数名、类型和长度的设定,并通过实例展示了两种实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SqlCommand.Parameters.add(name,value)

sc.Parameters.Add(parameterName,sqlDbType,size)  
  第一个是参数名,第二个是参数类型,第三个是长度  
  这是对应的你的SQL语句里的参数如:  
  SqlCommand   sc=new   SqlCommand("select   *   from   User   Where   UserID=@@UserID",conn)  
  sc.Parameters.Add(@@UserID,SqlDbType.Int,4)  
  sc.Parameters["@@UserID"].Value=1;  
     
  第二种方式:  
  SqlCommand   sc=new   SqlCommand("select   *   from   User   Where   UserID=@@UserID",conn)  
  sc.Parameters.Add(@@UserID,1)


本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/kanoalibby/archive/2009/04/30/4140746.aspx

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值