如果存在 [英] IF Exists

查看:65
本文介绍了如果存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个插入名字的程序,但是它必须检查表中是否存在该名称,如果存在,它应该返回一条消息,提示已经存在了.

存在,如果不存在,则插入应该继续.


这些是我的代码:


我的存储过程读取:

Hi all,

I have a stroed procedure to insertname, but it has to check if the name exists in the table, if it exist it should return a message that it already

exist, if it doesnt exist insertion should go on.


These are my codes:


My Stored procedure reads:



ALTER procedure dbo.InsertName @Cname nchar(10), @Address nchar(10), @ret varchar(100) output as if exists (select * from mytable where Cname=@Cname) begin select @ret as [ret] set @ret='insert failed. Record exists already' end else begin insert into mytable values (@Cname,@Address) select @ret as [ret] set @ret='insert successful' end

我的代码在这里:

My Code behind here:

protected void Button1_Click(object sender, EventArgs e)
    	{

        string constr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True";
        string result="";
        SqlConnection conn = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "InsertName";
        cmd.Parameters.AddWithValue("@Cname", this.TextBox1.Text);
        cmd.Parameters.AddWithValue("@Address", this.TextBox2.Text);
        cmd.Parameters.AddWithValue("@ret", result);
        cmd.Connection = conn;
        cmd.Parameters["@ret"].Direction = ParameterDirection.InputOutput;
        
        conn.Open();
        cmd.ExecuteNonQuery();
        Label1.Text = result;

	}


它可以工作,但是它不会在标签控件上显示该消息,请告诉我我做错了什么.

谢谢


.Neter


It works, but it doesn't display the message on the label control, please tell me what I have done wrong.

Thanks


.Neter

推荐答案

.Neter,您好,

我首先想告诉您,对于存储过程,您应该在某个sql论坛上启动线程.然后,我看到了您的C#代码.肯定在VB通用论坛中,这两个地方都是问这个问题的错误位置.
Hi .Neter,

i first wanted to tell you, that for stored procedures you should start a thread at some sql forum. Then i have seen your C# Code. Both in a VB General forum is for sure the wrong place to ask this question.


这篇关于如果存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆