0

私がしていることについての少しの背景。このコードに移動するクリック呼び出しがあるボタンがあります。

  static public DataSet shareFiles(TransitoryRegObj argTransRegObj)
{
    string sqlString = "do_share_files"; // it's a stored procedure
    SqlConnection cnn = new SqlConnection(masterConn);
    SqlCommand comm = new SqlCommand(sqlString, cnn);
    DataSet ds = new DataSet();

    try
    {
        cnn.Open();
        SqlCommand Comm = new SqlCommand(sqlString, cnn);
        Comm.CommandType = CommandType.StoredProcedure;


        comm.Dispose();
        cnn.Close();

        return ds;
    }
    catch (Exception ex)
    {
        // log here should anything go wrong with anything
        //  lblmessage.Text = "Error: " + ex.Message;



        if (comm != null)
            comm.Dispose();

        if (cnn != null)
            cnn.Close();

        DataTable dt = new DataTable("ExceptionTable");
        dt.Columns.Add("ExceptionMessage");
        dt.Rows.Add(ex.Message);
        ds = new DataSet();
        ds.Tables.Add(dt);
        return ds;
    }
}

コードは正常に実行されますが、データベースには何も書き込まれません。これがdo_share_filesストアドプロシージャです。

  ALTER PROCEDURE [dbo].[do_share_files] 
   --@device_id bigint, @user_id bigint, @file_name varchar(50),@full_up_path varchar(50), @upLength varchar(30)
    --,@mime_type varchar(20), @filedate varchar(30)

    AS
    BEGIN
        insert into [user_files] (device_id, user_id, original_name, original_path, up_path, content_type, up_dt)
        values (17, 30, 'test.pg', 'test.pg', 'test.pg','test.pg', '2012-11-15 03:58:06.043')


    END

ストアドプロシージャで実行しようとしているだけなので、今のところ静的な値があります。私はasp.netを初めて使用し、何が間違っているのかわかりません。どんな助けでもいただければ幸いです。

ありがとう!

4

2 に答える 2

0

あなたはこれから始めることができます:

static public DataSet shareFiles(TransitoryRegObj argTransRegObj)
{
    string sqlString = "do_share_files"; // it's a stored procedure
    DataSet ds = new DataSet();

    try
    {
        using (var cnn = new SqlConnection(masterConn))
        { 
            SqlCommand comm = new SqlCommand(sqlString, cnn);
            comm.CommandType = CommandType.StoredProcedure;
            cnn.Open();
            comm.ExecuteNonQuery ();

要約する:

  1. Commcommは異なるコマンドです。
  2. procを実行するには、ExecuteNonQueryまたは他のExecuteメソッドを呼び出す必要があります。
于 2012-12-04T19:07:28.843 に答える
0

あなたのコードにはほとんど間違いがありません1.なぜこの行を2回使用しているのか理解できません

SqlCommand comm = new SqlCommand(sqlString, cnn);

2.主な問題である手順を実行しなかった

static public DataSet shareFiles(TransitoryRegObj argTransRegObj)
{
try
{
string sqlString = "do_share_files"; // it's a stored procedure
SqlConnection cnn = new SqlConnection(masterConn);
SqlCommand comm = new SqlCommand(sqlString, cnn);
DataSet ds = new DataSet();  
    cnn.Open();
    Comm.CommandType = CommandType.StoredProcedure;
    comm.ExecuteNonQuery();
    comm.Dispose();
    cnn.Close();

    return ds;
}
catch (Exception ex)
{
   //something here
}

}

于 2012-12-04T19:10:21.247 に答える