如何检查数据库表中是否存在值 [英] how to check if a value exists in DB table
本文介绍了如何检查数据库表中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查数据库表中是否存在值,并将布尔值返回给vb.net中的变量?
how to check if a value exists in a DB table and return a Boolean value to variable in vb.net??
推荐答案
这并执行功能以获取所需的输出
you can create a function like this and execute the function to get your desired output
create function fnCheckExists()
returns bit
as
begin
declare @val as bit=0;
if exists (select yourColumn from yourTable)
begin
set @val=1;
end
else
begin
set @val=0;
end
return @val
end
像这样尝试
Hi Try like this
private void Form4_Load(object sender, EventArgs e)
{
int Company_Id = 3;
Boolean id = Convert.ToBoolean(existCompanyId(Company_Id));
}
public int existCompanyId(int Company_Id)
{
string SQLConnectionStr = "Password=password;Connection Timeout=0;Persist Security
Info=False;User ID=userid;Initial Catalog=databasename;Data Source=servername;";
SqlConnection sqlConnection = new SqlConnection(SQLConnectionStr);
int CId;
string TableExists = "IF EXISTS (select * from Company WHERE CompanyId ='" +
Company_Id + "') select 1 ELSE select 0";
try
{
using (SqlConnection spContentConn = new SqlConnection(SQLConnectionStr))
{
spContentConn.Open();
using (SqlCommand TableExistsQry = new SqlCommand(TableExists, spContentConn))
{
TableExistsQry.CommandTimeout = 0;
TableExistsQry.CommandType = CommandType.Text;
CId = System.Convert.ToInt32(TableExistsQry.ExecuteScalar());
}
spContentConn.Close();
}
return CId;
}
catch (Exception ex)
{
throw ex;
}
}
如果表中存在公司ID 3,我们将获得ID为true的ID,否则将获得其他明智的ID的false.
If the Company Id 3 exists in the table we will get id as true other wise id as false.
这篇关于如何检查数据库表中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文