本文介绍了如何在Sql数据库中选择与下拉组合匹配的Certian行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 道歉没有正确地问我的问题 我的意思是如何选择与下拉组合匹配的Certian行一个Sql数据库 这就是我所做的。 /// //我点击事件上的按钮 private void ribbonButton_Enter_Click( object sender,EventArgs e) { /// //使用try to catch errors 尝试 { /// //从config加载连接字符串 配置MSystemConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); string glConnectionString = MSystemConfig.ConnectionStrings.ConnectionStrings [ TrueWeigh.Properties.Settings.DatabaseConnectionString]的ConnectionString。 使用(SqlConnection Conn = new SqlConnection(ConnectionString) ) { /// //打开SQL连接 Conn.Open(); /// //这就是我遇到问题的地方 /// // / // //如果我尝试在数据库中查找数字,但是没有ascii /// // /// // Visual Studio返回null(空引用未处理 string cmdString = SELECT * FROM Users WHERE UserName = +( '& comboBox_User_Name.SelectedText&')。ToString(); 使用(SqlCommand sqlcmd = new SqlCommand(cmdString,Conn)) { SqlDataReader breader = sqlcmd.ExecuteReader(); if (breader.HasRows) { while (breader.Read()) { Console.WriteLine(GlobalInfoUser.glUserID = breader [ UserID]。ToString()); } } breader.Close(); } } } catch (SqlException) { } 解决方案 嗯......这是相当奇怪的代码。 .. string cmdString = SELECT * FROM Users WHERE UserName = +( ' & comboBox_User_Name.SelectedText&')。ToString(); 您可能想说: string cmdString = SELECT * FROM Users WHERE UserName =' + comboBox_User_Name.SelectedText + '; 但这是一个糟糕的想法 - 部分是因为它鼓励SQL注入攻击,这可能会破坏或破坏您的数据库(使用参数化查询),部分原因是如果ComboBox中没有选择任何内容,它将生成无效的SQL命令。 br /> 您应首先检查价值: 如果(!string.IsNullOrWhiteSpace(comboBox_User_Name.SelectedText)) { // do你的代码 } 并为SQLCommand使用参数化查询。 string cmdString = SELECT * FROM Users WHERE UserName = @ UN; 使用(SqlCommand sqlcmd = new SqlCommand(cmdString,Conn)) { sqlcmd.Parameters.AddWithValue( @ UN,comboBox_User_Name.SelectedText); SqlDataReader breader = sqlcmd.ExecuteReader(); 首先 - 不要使用字符串连接来创建SQL命令 - 这是一个安全问题! /> 使用 SqlParameter [ ^ ]而不是...... 用 SELECT * > SELECT field1 , field2 ,... fieldn ... Apologies for not asking my question correctlywhat I meant is How Do I Select Certian Rows That Match a dropdown combo In A Sql DatabaseThis is what I have done. ///// My button on click event private void ribbonButton_Enter_Click(object sender, EventArgs e) {/////using try to catch errors try {/////loading connection string from configConfiguration MSystemConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);string glConnectionString = MSystemConfig.ConnectionStrings.ConnectionStrings["TrueWeigh.Properties.Settings.DatabaseConnectionString"].ConnectionString; using (SqlConnection Conn = new SqlConnection(ConnectionString)) {/////Opening SQL connection Conn.Open();///// This is where I have a problem//////////if I try and find numbers in the database it works but no ascii////////// Visual studio return with null(Null reference was unhandled string cmdString = "SELECT * FROM Users WHERE UserName =" + ("' & comboBox_User_Name.SelectedText & '").ToString(); using (SqlCommand sqlcmd = new SqlCommand(cmdString , Conn)) { SqlDataReader breader = sqlcmd.ExecuteReader(); if (breader.HasRows) { while (breader.Read()) { Console.WriteLine(GlobalInfoUser.glUserID = breader["UserID"].ToString()); } } breader.Close(); } } } catch (SqlException) { } 解决方案 Um...that's rather odd code...string cmdString = "SELECT * FROM Users WHERE UserName =" + ("' & comboBox_User_Name.SelectedText & '").ToString();You probably meant to say:string cmdString = "SELECT * FROM Users WHERE UserName ='" + comboBox_User_Name.SelectedText + "'";But that is a poor idea - partly because it encourages SQL Injection attacks, which can damage or destroy your database (use parametrized queries instead) and partly because if there is nothing selected in the ComboBox it will generate an invalid SQL command.You should check the value first:if (!string.IsNullOrWhiteSpace(comboBox_User_Name.SelectedText)) { // do your code }And use a parametrized query for the SQLCommand.string cmdString = "SELECT * FROM Users WHERE UserName=@UN";using (SqlCommand sqlcmd = new SqlCommand(cmdString , Conn)) { sqlcmd.Parameters.AddWithValue("@UN", comboBox_User_Name.SelectedText); SqlDataReader breader = sqlcmd.ExecuteReader();First - do not use string concatenation to create SQL commands - it's a security problem!Use SqlParameter[^] instead...Replace SELECT * with SELECT field1, field2, ... fieldn... 这篇关于如何在Sql数据库中选择与下拉组合匹配的Certian行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 05:14