如何将多个数据类型数据作为参数传递给ms sql存储过程 [英] How to pass multiple data type data as parameter to ms sql stored procedure

查看:79
本文介绍了如何将多个数据类型数据作为参数传递给ms sql存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手。现在我正在尝试Android演示应用程序。我通过从ANDROID发送多个数据类型数据作为参数来访问存储过程。



创建Rest Web服务以访问来自DB的数据和下面提到的接受3参数的方法并返回字符串。



 使用系统; 
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Text;
使用 Newtonsoft.Json;

命名空间 JSONWebAPI
{
public class ServiceAPI:IServiceAPI
{
private SqlConnection SqlCon;
private SqlCommand SqlCmd;
private SqlDataAdapter SqlAdapt;
private DataSet ds;
private SqlDataReader SqlReader;
private StringBuilder Sb;

private dynamic 结果;

public ServiceAPI()
{
SqlCon = DBConnect.getConnection();
}

public 字符串 Getdata( String strSqlQuery,SqlParameter [] arrSqlParam, Boolean IsStoredProcedure)
{
使用(SqlCon)
{
if (SqlCon.State == ConnectionState.Closed || SqlCon.State = = ConnectionState.Broken)
SqlCon.Open();
SqlCmd = new SqlCommand(strSqlQuery,SqlCon);
if (IsStoredProcedure)
{
SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.Parameters.AddRange(arrSqlParam);
}
SqlCmd.ExecuteNonQuery();
SqlAdapt = new SqlDataAdapter(SqlCmd);
ds = new DataSet();
SqlAdapt.Fill(ds);

结果= ConvertTableToJson(ds);

SqlAdapt.Dispose();
SqlCmd.Dispose();
SqlCon.Dispose();
}
return 结果;
}

private String ConvertTableToJson(DataSet dsDownloadJson)
{
return JsonConvert.SerializeObject(dsDownloadJson,Formatting.Indented);
}
}
}





我搜索了谷歌但仍然没有得到解决方案。如何实现这一点请指导我。

解决方案

很像 Console.WriteLine SqlParameter 构造函数被重载,所有重载的第一个参数是参数的名称,后跟需要的其他参数来指定其类型和值。因此,您对 SqlParameter 数组进行维度,并为每个查询参数填充一个新的 SqlParameter 对象。虽然所有类型都是( SqlParameter ),但每个实例更像是一个 NameValueCollection 项,你可以混合使用根据需要匹配类型。



有关其他信息,请参阅 https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v = vs.110)的.aspx [ ^ ]

I am new to android development. Now i'm trying android demo application. I struck to access stored procedure by sending multiple data type data as parameter from ANDROID.

Created Rest web service to access data from DB and method mentioned below which accept 3 parameter and return string.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Newtonsoft.Json;

namespace JSONWebAPI
{
    public class ServiceAPI : IServiceAPI
    {
        private SqlConnection SqlCon;
        private SqlCommand SqlCmd;
        private SqlDataAdapter SqlAdapt;
        private DataSet ds;
        private SqlDataReader SqlReader;
        private StringBuilder Sb;

        private dynamic Result;

        public ServiceAPI()
        {
            SqlCon = DBConnect.getConnection();
        }

        public String Getdata(String strSqlQuery, SqlParameter[] arrSqlParam, Boolean IsStoredProcedure)
        {
            using (SqlCon)
            {
                if (SqlCon.State == ConnectionState.Closed || SqlCon.State == ConnectionState.Broken)
                    SqlCon.Open();
                SqlCmd = new SqlCommand(strSqlQuery, SqlCon);
                if (IsStoredProcedure)
                {
                    SqlCmd.CommandType = CommandType.StoredProcedure;
                    SqlCmd.Parameters.AddRange(arrSqlParam);
                }
                SqlCmd.ExecuteNonQuery();
                SqlAdapt = new SqlDataAdapter(SqlCmd);
                ds = new DataSet();
                SqlAdapt.Fill(ds);

                Result = ConvertTableToJson(ds);

                SqlAdapt.Dispose();
                SqlCmd.Dispose();
                SqlCon.Dispose();
            }
            return Result;
        }

        private String ConvertTableToJson(DataSet dsDownloadJson)
        {
            return JsonConvert.SerializeObject(dsDownloadJson, Formatting.Indented);
        }
    }
}



I have searched google but still I didn't get solution. How to achieve this please guide me on this.

解决方案

Much like Console.WriteLine, the SqlParameter constructor is overloaded, and the first argument to all overloads is the name of the parameter, followed by additional arguments as needed to specify its type and value. So, you dimension your SqlParameter array, and fill it with a new SqlParameter object for each query parameter. Although all are of a type (SqlParameter ), each instance is more like a NameValueCollection item, and you can mix and match types as needed.

For additional info, please see https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx[^]


这篇关于如何将多个数据类型数据作为参数传递给ms sql存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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