如何在C#中按条件获取价值 [英] how do get value by condition in c#

查看:78
本文介绍了如何在C#中按条件获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个用于在投票表中保存记录的存储过程
(我想要投票代码Dublicate 客户代码不存在存储过程向C#发送一个数字,并在C#中使用我从返回的存储过程值中使用)

hi all

i have a Stored Procedure That For Save Record In Vote Table
(i want if vote code Dublicate or Customer Code is not exists Stored procedure Send A number To C# And In C# I Use From Returned Stored Procedure Value )

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




ALTER PROCEDURE [dbo].[Votes_Insert]
    (
    @cus_id                     [bigint],
    @vote_id                        [bigint],
    @vote_comment                       [nvarchar](500)
    )
AS

if exists (select * from Votes where vote_id=@vote_id)
return '3'

if not exists (select * from Customers where cus_id=@cus_id)
return '2'


INSERT INTO Votes
    (
    cus_id,
    vote_id,
    vote_comment
    )
VALUES
    (
    @cus_id,
    @vote_id,
    @vote_comment
    )
return '1'



我在csharp中使用此代码来使用它:



i use this code in csharp to use it :

private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {


                if (txt_cus_code.Text == "" || txt_vote_code.Text == "" || txt_vote_body .Text == "")
                {
                    btn_Result.Text = "Please Enter All Information";
                    btn_Result.ForeColor = Color.Red;
                    btn_Result.PulseSpeed = 100;
                    btn_Result.Pulse(14);
                    btn_Result.Text = "";
                }

                else
                {

                    SqlConnection MySqlConnection = new SqlConnection(ConnectionMain.strconnection);
                    SqlCommand MySqlCommand = MySqlConnection.CreateCommand();
                    MySqlCommand.CommandText = "Votes_Insert";
                    MySqlCommand.CommandType = CommandType.StoredProcedure;

                    MySqlCommand.Parameters.Add("@cus_id", SqlDbType.BigInt, 8).Value = Convert.ToInt64(txt_cus_code .Text);
                    MySqlCommand.Parameters.Add("@vote_id", SqlDbType.BigInt , 8).Value = Convert.ToInt64(txt_vote_code.Text);
                    MySqlCommand.Parameters.Add("@vote_comment", SqlDbType.NVarChar, 500).Value = txt_vote_body .Text;
                   
                    if (MySqlConnection.State != ConnectionState.Open)
                    {
                        MySqlConnection.Open();
                    }

                    int result = MySqlCommand.ExecuteNonQuery();

                    if (MySqlConnection.State != ConnectionState.Closed)
                    {
                        MySqlConnection.Close();
                    }
                    if (result == 1)
                    {
                        btn_Result.Text = "inforation saved successfull ...";
                        btn_Result.ForeColor = Color.Green;
                        btn_Result.PulseSpeed = 100;
                        btn_Result.Pulse(14);
                        btn_Result.Text = "";
                    }
                    else if (result ==3)
                    {
                        btn_Result.Text = "vote code is dublicate";
                        btn_Result.ForeColor = Color.Red;
                        btn_Result.PulseSpeed = 100;
                        btn_Result.Pulse(14);
                        btn_Result.Text = "";

                    }
                    else if (result == 2)
                    {
                        btn_Result.Text = "customer not exists";
                        btn_Result.ForeColor = Color.Red;
                        btn_Result.PulseSpeed = 100;
                        btn_Result.Pulse(14);
                        btn_Result.Text = "";

                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }

            vote_Load (this, e);
      }



此代码保存记录,但是当我输入重复的投票ID时不显示任何消息

我该怎么办?

请帮助我

非常感谢



This Codes Save Record , But When I Enter Duplicate Vote Id Dont Show Any Message

What I Do ?

Please Help Me

Thanks A Lot

推荐答案

首先,SP的返回值与switch语句中的一个预期类型之间存在类型差异;最后一个期望一个int,并且SP返回一个char(返回"3").

此外,ExecuteNonQuery()方法根据定义返回所影响的行数.因此,保存工作正常.

要解决它,您必须更改SP eq的返回值.返回3而不是返回"3",并添加参数以检索SP的返回值.


First there is the discrepancy in type between the return value of the SP and the one expected type in the switch statement; the last expects an int and the SP return a char (return ''3'').

Furthermore the ExecuteNonQuery() method returns by definition the number of rows effected. Therefore the save works correctly.

To solve it you must change the return value of the SP eq. return 3 in stead of return ''3'' and add a parameter to retrieve the return value of the SP.


// Return value as parameter
SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
MySqlCommand.Parameters.Add(returnValue);
.
.
.
// Execute the stored procedure
MySqlCommand.ExecuteNonQuery();
.
.
.
int result =  Convert.ToInt32(returnValue.Value);



问候
Piet



Regards
Piet


不是直接答案,而是一些提示

而不是这个
Not a direct answer but some tips

Rather than this
if (MySqlConnection.State != ConnectionState.Closed)
{
  MySqlConnection.Close();
}



您应该利用using block



You should be making use of using block

using(SqlConnection conn = new SqlConnection(...))
{

}



这样即使退出异常,也可以确保关闭连接对象并对其进行处置.

使用起来更高效



This will ensure the connection object closed and disposed of when the block is exited, even if an exception occurs.

It is more performant to use

txt_cus_code.Length == 0


而不是这个


rather then this

txt_cus_code.Text == ""


这篇关于如何在C#中按条件获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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