Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回用于在C中处理的布尔值的sql命令_C#_Sql_Database_Ado.net_Sqlcommand - Fatal编程技术网

C# 返回用于在C中处理的布尔值的sql命令

C# 返回用于在C中处理的布尔值的sql命令,c#,sql,database,ado.net,sqlcommand,C#,Sql,Database,Ado.net,Sqlcommand,是否可以从sql命令返回布尔值,然后在C中使用它 我目前有一个数据库,在其中插入条目。我想在插入之前知道条目是否存在,如果存在,请做些什么 private void create_user_Click(object sender, EventArgs e) { int exist; using (DbConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFile

是否可以从sql命令返回布尔值,然后在C中使用它

我目前有一个数据库,在其中插入条目。我想在插入之前知道条目是否存在,如果存在,请做些什么

private void create_user_Click(object sender, EventArgs e)
{
    int exist;
    using (DbConnection connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\toilet\source\repos\WindowsFormsApp2\WindowsFormsApp1\Database1.mdf;Integrated Security=True"))
    {
        connection.Open();
        using (DbCommand command = new SqlCommand("WHERE EXIST (SELECT id FROM [information] WHERE id == @given_id)"))
        {
            command.Parameters.Add(new SqlParameter("@given_id", create_user_username_textbox.Text));
            command.Connection = connection;
            exist = command.ExecuteNonQuery();
        }
        MessageBox.Show(Convert.ToString(exist));
    }

    //login_screen login = new login_screen();
    //this.Hide();
    //login.ShowDialog();
    //this.Close();

}
执行此操作会在SqlCommand中的位置附近出现语法错误

请尝试使用有效的sql语句,并使用ExecuteScalar获取一些值进行比较(如果存在)。对于示例:

 using (DbCommand command = new SqlCommand("SELECT 1 FROM [information] WHERE id = @given_id"))
 {
     command.Parameters.Add(new SqlParameter("@given_id", create_user_username_textbox.Text));
     command.Connection = connection;

     // get the result from the sql statement 
     var result = command.ExecuteScalar();

     // if the result is not null, convert it to an integer and compare it. Otherwise it is false (no records).
     exists = result != null ? (int) result > 0 : false;

 }
尝试使用有效的sql语句,并使用ExecuteScalar获取一些值以进行比较(如果存在)。对于示例:

 using (DbCommand command = new SqlCommand("SELECT 1 FROM [information] WHERE id = @given_id"))
 {
     command.Parameters.Add(new SqlParameter("@given_id", create_user_username_textbox.Text));
     command.Connection = connection;

     // get the result from the sql statement 
     var result = command.ExecuteScalar();

     // if the result is not null, convert it to an integer and compare it. Otherwise it is false (no records).
     exists = result != null ? (int) result > 0 : false;

 }

使用以下查询检查可用性

IF EXIST (SELECT id FROM [information] WHERE id = @given_id) 
SELECT CAST(1 AS BIT)
ELSE 
SELECT CAST(0 AS BIT)

使用以下查询检查可用性

IF EXIST (SELECT id FROM [information] WHERE id = @given_id) 
SELECT CAST(1 AS BIT)
ELSE 
SELECT CAST(0 AS BIT)
只用

SELECT id FROM [information] WHERE id == @given_id
并按如下方式调用SqlCommand:

object o = cmd.ExecuteScalar();
您可以设置一个布尔值,如:

bool valueExists = o != null && o != DBNull.Value;
只用

SELECT id FROM [information] WHERE id == @given_id
并按如下方式调用SqlCommand:

object o = cmd.ExecuteScalar();
您可以设置一个布尔值,如:

bool valueExists = o != null && o != DBNull.Value;

是的,但是这里没有有效的SQL命令,只是查询的一部分。X-Y。。。你是想做一个翻转吗?更新(如果存在),插入else@LiamOP通常似乎不熟悉SQL。请将查询更改为从[information]中选择1,其中id=@given_id。检查它是否返回1。如果不是,则为false。为什么要返回布尔值?如果查询返回单个ID,则您已经知道存在一条记录。您甚至可能不需要向服务器返回任何内容-您可以编写适当的合并、更新或插入语句是的,但此处没有有效的SQL命令,只是查询的一部分。X-Y。。。你是想做一个翻转吗?更新(如果存在),插入else@LiamOP通常似乎不熟悉SQL。请将查询更改为从[information]中选择1,其中id=@given_id。检查它是否返回1。如果不是,则为false。为什么要返回布尔值?如果查询返回单个ID,则您已经知道存在一条记录。您甚至可能不需要向服务器返回任何内容—您可以编写适当的MERGE、UPDATE或INSERT语句1。SQL Server不支持==。2.在这种情况下,这可能没问题,但选择count*只是为了确定表中是否存在一条记录,这可能会导致性能下降,如果where子句可能返回多条记录,那么我在评论中的建议也是如此。Yes@ZoharPeled,感谢您的反馈。我在上面加了一些调整。SQL Server不支持==。2.在这种情况下,这可能没问题,但选择count*只是为了确定表中是否存在一条记录,这可能会导致性能下降,如果where子句可能返回多条记录,那么我在评论中的建议也是如此。Yes@ZoharPeled,感谢您的反馈。我在上面加了一些调整。不需要像这样的东西。有结果意味着有记录。1它也不是布尔值。@panagiotis kanavos同意。只是修改了问题中使用的查询。不需要返回类似的内容。有结果意味着有记录。1它也不是布尔值。@panagiotis kanavos同意。刚刚修改了问题中使用的查询。