如何返回字符串值 [英] How Can I Return A String Value

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

问题描述

public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
         {
            SqlConnection cn = new SqlConnection(connection());
            try
            {
                //initialisation of datatable, Sql connection and sql command
                DataSet dt = new DataSet();

                SqlCommand cmd = new SqlCommand(storedprocedure, cn);
                cmd.CommandType = CommandType.StoredProcedure;

                //opens the db connection
                if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
                    cn.Open();

                //if sql param is not null then gets the parameters from that
                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                        cmd.Parameters.Add(param);
                }
                cmd.CommandTimeout = 5000;
                //executes the command and fills it into the datatable
              
                return string;
            }

            catch (Exception ee)
            {
                
                return null;
            }
            finally
            {
                cn.Close();
            }
        }



返回时显示错误

如果false类型成功,则返回2个成功或失败的任何字符串


its showing error on return
it should return 2 thins if success true of false type or any string for success or failure

推荐答案

请参阅以下更改并确认我: -

公共字符串ExecuteScalar(String storedprocedure,params SqlParameter [] arrParam)

{

SqlConnection cn = new SqlConnection(connection());

try

{

//初始化datatable,Sql连接和sql命令

DataSet dt = new DataSet();



SqlCommand cmd = new SqlCommand(storedprocedure,cn);

cmd.CommandType = CommandType.StoredProcedure;



//打开数据库连接

if(cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)

cn.Open();



//如果sql param不为null,则从中获取参数

if(arrParam!= null)

{

foreach(arrParam中的SqlParameter参数)

cmd.Parameters.Add(param);

}

cmd.CommandTimeout = 5000;

//执行命令并将其填入数据表中



返回成功;

}



catch(例外ee)

{



返回错误;

}

终于

{

cn.Close();

}

}
See below changes and confirm me:-
public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
{
SqlConnection cn = new SqlConnection(connection());
try
{
//initialisation of datatable, Sql connection and sql command
DataSet dt = new DataSet();

SqlCommand cmd = new SqlCommand(storedprocedure, cn);
cmd.CommandType = CommandType.StoredProcedure;

//opens the db connection
if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
cn.Open();

//if sql param is not null then gets the parameters from that
if (arrParam != null)
{
foreach (SqlParameter param in arrParam)
cmd.Parameters.Add(param);
}
cmd.CommandTimeout = 5000;
//executes the command and fills it into the datatable

return "success";
}

catch (Exception ee)
{

return "error";
}
finally
{
cn.Close();
}
}


public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
         {
string _result = "failure";
            SqlConnection cn = new SqlConnection(connection());
            try
            {
                //initialisation of datatable, Sql connection and sql command
                DataSet dt = new DataSet();
 
                SqlCommand cmd = new SqlCommand(storedprocedure, cn);
                cmd.CommandType = CommandType.StoredProcedure;
 
                //opens the db connection
                if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
                    cn.Open();
 
                //if sql param is not null then gets the parameters from that
                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                        cmd.Parameters.Add(param);
                }
                cmd.CommandTimeout = 5000;
                //executes the command and fills it into the datatable
              
               _result = "success";
            } 
            catch (Exception e)
            {                
               _result = "exception";
            }
            finally
            {
                cn.Close();
            }
return _result;
        }


你必须返回一个字符串,而不是字符串。

You have to return a string and not "string".
public string ExecuteScalar(String storedprocedure, params SqlParameter[] arrParam)
         {
            SqlConnection cn = new SqlConnection(connection());
            try
            {
                //initialisation of datatable, Sql connection and sql command
                DataSet dt = new DataSet();
 
                SqlCommand cmd = new SqlCommand(storedprocedure, cn);
                cmd.CommandType = CommandType.StoredProcedure;
 
                //opens the db connection
                if (cn.State == ConnectionState.Closed || cn.State == ConnectionState.Broken)
                    cn.Open();
 
                //if sql param is not null then gets the parameters from that
                if (arrParam != null)
                {
                    foreach (SqlParameter param in arrParam)
                        cmd.Parameters.Add(param);
                }
                cmd.CommandTimeout = 5000;
                //executes the command and fills it into the datatable
              
                return "success";
            }
 
            catch (Exception ee)
            {
                
                return "failer";
            }
            finally
            {
                cn.Close();
            }
        }


这篇关于如何返回字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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