检查我的数据库中是否存在文本C# [英] Check if text exist in my database C#
问题描述
这就是我现在写的内容
<
connstring = SERVER = 188.121.43.9; PORT = 3306; DATABASE = mydbname; UID = mydbusername; PASSWORD = s1996;;
MySqlConnection conn = new MySqlConnection(connstring);
MySqlCommand command = conn.CreateCommand();
command.CommandText =从dbtable中选择全部;
>
这就是我的数据库的样子
<
uname upass
test 1900
test2 1999
>
i有textbox1
我想要的是
例如:
chek如果textbox1.text存在于列uname show MessageBox.Show(存在)
我尝试过:
嘿伙计们,任何人都可以帮我检查数据库中是否存在文本
这就是我现在写的内容
<
connstring =SERVER = 188.121.43.9; PORT = 3306; DATABASE = mydbname; UID = mydbusername; PASSWORD = s1996;;
MySqlConnection conn = new MySqlConnection(connstring);
MySqlCommand command = conn.CreateCommand();
command.CommandText =从dbtable中选择全部;
>
这就是我的数据库看起来像
<
uname upass
test 1900
test2 1999
>
i有textbox1
我想要的是
例如:
chek如果textbox1.text存在于列uname上显示MessageBox.Show(exists)
我会使用ExecuteScalar来提高效率......像这样:private void btnNext_Click( object sender,EventArgs e)
{
if (comboBox1.SelectedIndex == -1) return ;
使用( var conn = new SqlConnection(connstring))
{
conn.Open();
var cmd = new SqlCommand
{
CommandText = 从dbtable中选择count(*),其中UPPER([name])= @ name,
Connection = conn
};
cmd.Parameters.AddWithValue( @ name,comboBox1.SelectedItem.ToString() );
var res =( int )cmd.ExecuteScalar();
if (res > 0 )
MessageBox.Show( exists);
}
}注意事项:
- 我使用使用
参见< a href =https://msdn.microsoft.com/en-us/library/ms254507(v=vs.110).aspx>建立连接 [ ^ ]Quote:当代码退出块时,Visual Basic或C#中的Using块会自动处理连接,即使在未处理的异常情况下也是如此。
- 我使用了ExecuteScalar方法,因为它需要更少的代码和更少的资源 - 请参阅SqlCommand.ExecuteScalar Method(System.Data.SqlClient) [ ^ ]
- 我检查确保在尝试使用该值之前在comboBox上实际选择了某些内容。
< pre> String connstring = < span class =code-string> SERVER = 188.121.43.9; PORT = 3306; DATABASE = mydbname; UID = mydbusername; PASSWORD = s1996;;
SqlConnection conn = new SqlConnection(connstring);
SqlCommand command = conn.CreateCommand();
command.CommandText = 从dbtable中选择*其中name = @ name;
command.Parameters.AddWithValue( @ name,comboBox1.SelectedValue);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0 )
{
MessageBox.Show( exists);
}
hey guys can anyone help me to check if a text exist in database
thats what im writting at this moment
<
connstring = "SERVER=188.121.43.9;PORT=3306;DATABASE=mydbname;UID=mydbusername;PASSWORD=s1996;"; MySqlConnection conn = new MySqlConnection(connstring); MySqlCommand command = conn.CreateCommand(); command.CommandText = "Select all from dbtable";
>
this is how my DB looks like
<
uname upass test 1900 test2 1999
>
i have textbox1
what i want is
for example :
chek if textbox1.text exist at column uname show MessageBox.Show("exist")
What I have tried:
hey guys can anyone help me to check if a text exist in database thats what im writting at this moment < connstring = "SERVER=188.121.43.9;PORT=3306;DATABASE=mydbname;UID=mydbusername;PASSWORD=s1996;"; MySqlConnection conn = new MySqlConnection(connstring); MySqlCommand command = conn.CreateCommand(); command.CommandText = "Select all from dbtable"; > this is how my DB looks like < uname upass test 1900 test2 1999 > i have textbox1 what i want is for example : chek if textbox1.text exist at column uname show MessageBox.Show("exist")
I would use ExecuteScalar to be more efficient... like this:private void btnNext_Click(object sender, EventArgs e) { if (comboBox1.SelectedIndex == -1) return; using (var conn = new SqlConnection(connstring)) { conn.Open(); var cmd = new SqlCommand { CommandText = "select count(*) from dbtable where UPPER([name])=@name", Connection = conn }; cmd.Parameters.AddWithValue("@name", comboBox1.SelectedItem.ToString()); var res = (int)cmd.ExecuteScalar(); if (res > 0) MessageBox.Show("exist"); } }Things to note:
- I've usedusing
see Establishing the Connection[^]Quote:The Using block in Visual Basic or C# automatically disposes of the connection when the code exits the block, even in the case of an unhandled exception.
- I've used the ExecuteScalar method as it requires less code and fewer resources - see SqlCommand.ExecuteScalar Method (System.Data.SqlClient)[^]
- I check to ensure there is something actually selected on the comboBox before attempting to use the value.
<pre>String connstring = "SERVER=188.121.43.9;PORT=3306;DATABASE=mydbname;UID=mydbusername;PASSWORD=s1996;"; SqlConnection conn = new SqlConnection(connstring); SqlCommand command = conn.CreateCommand(); command.CommandText = "Select * from dbtable where name=@name"; command.Parameters.AddWithValue("@name", comboBox1.SelectedValue); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows.Count > 0) { MessageBox.Show("exist"); }
这篇关于检查我的数据库中是否存在文本C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!