【问题标题】:.Net SQL Server connection simple class with procedure.Net SQL Server 连接简单类与过程
【发布时间】:2016-12-04 09:08:20
【问题描述】:

我需要用 C# 编写简单的类。我有 2 张桌子。但为了理解我只使用了一张桌子。

public string[] GetCustOrders(int CustomerCODE)
{
    SqlConnection myConn = new SqlConnection("server=(local);Initial Catalog=dbName;Integrated Security=True");

    SqlDataAdapter myData = new SqlDataAdapter("CustOrdersOrdersDetails", myConn);
    myData.SelectCommand.CommandType = CommandType.StoredProcedure;
    myData.SelectCommand.Parameters.Add(new SqlParameter("@CustomerCODE", SqlDbType.Int, 0));
    myData.SelectCommand.Parameters["@CustomerCODE"].Value = CustomerCODE;

    //  string[] as = string[6];
    string[] as1 = string[3];
    //       string[] as2 = string[3];

    DataSet ds = new DataSet();
    myData.Fill(ds);

    return as;
}

还有我的存储过程代码:

CREATE PROCEDURE CustOrdersOrdersDetails 
    @CustomerCODE int
AS
    SELECT 
        Name, 
        Action,
        Comments
    FROM 
        System2
    WHERE 
        Code = @CustomerCODE

存储过程将从表中仅选择 1 行。但是我不t know how fromSqlDataAdapter`获取这一行的每个单元格的值并将这个值作为我的数组作为1。

例如,"as[1]=" 和之后 = 我需要从所选行中给出 Action 的值。

我知道这很简单,但我认为......并且认为,我可以这样做,只需将我的行的所有值提供给 DataSet,然后将每个单元格的每个值提供给我的数组中的每个字符串。但是我托盘并不能完成这门课......

谁能帮忙?

【问题讨论】:

    标签: c# sql .net sql-server stored-procedures


    【解决方案1】:
    int i = 0;    
    // For each table in the DataSet, print the row values.
    foreach(DataTable table in ds.Tables)
    {
        foreach(DataRow row in table.Rows)
        {
            foreach (DataColumn column in table.Columns)
            {
                as[i]=row[column]);
                i++;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多