本文介绍了如何检查数据库中是否存在值,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我在Windows窗体中有一个文本框.我的问题是,如何检查通过数据库在文本框中输入的现有文本?请举一个例子.
Hi all,
I have a text box in a windows form. My question is, how to check existing text which is entered in text box through database? Please reply me with an example.
推荐答案
Create Procedure FindString(<br />
@MyString nvarchar(50))<br />
As<br />
Begin<br />
Select * From MyTable<br />
Where Value = @MyString<br />
End
在.NET中创建类,例如:
Make a Class in .NET For example :
public class ReadData
{
public bool FindString(string myString)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = "Server=..."; //Your connection string
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "FindString";
command.Parameters.AddWithValue("@MyString", myString);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
return true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
return false;
}
}
使用该类.例如:
Use the class. for example :
ReadData r = new ReadData();
if (r.FindString("Shahingg"))
MessageBox.Show("I Found it!");
else
MessageBox.Show("I can't Find it!");
不要忘记,您的Class需要以下名称空间:
Do not forget that you need below namespaces for your Class :
using System.Data.SqlClient;
using System.Data;
祝你好运
Good Luck
CREATE PROCEDURE Procedure_Name
@mystring varchar(100),
@isExist bit out
AS
BEGIN
if exists(select column1 from tblTable1 where column1=@mystring)
begin
select @isExist=1
end
else
begin
select @isExist=0
end
END
GO
然后编写方法以检查该值是否存在
那就是@ isExist = 1表示该值存在.否则不存在.
希望这会对您有所帮助.
then write the method to check whether the value exist or not
thatis if @isExist=1 that means the value exist.otherwise not..
Hope this may help you..
If(exists(select * from table where column=@clounm))
Begin
Selct ''true''
END
ELSE
BEGIN
select ''false''
END
这篇关于如何检查数据库中是否存在值,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!