SQL插入输出 [英] SQL Output Inserted

查看:164
本文介绍了SQL插入输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个按钮的页面,一个记录起始时间,一个记录的结束时间。

的开始时间按钮执行SQL INSERT。
在这一点上我需要抓住主键的创建。要做到这一点,我想用SQL命令(输出插入)。

那么,点击停止的时候,该行应使用从一开始的主键在where子句中与停止时间更新。
我相信,插入SQL是正确的,但我不知道如何通过主键下一个命令。

code转储,与我有什么至今。

  VAR命令1 =INSERT INTO [时间]([开始时间],[工作订单])输出INSERTED.PrimaryKey VALUES(@starttime,@Work_Order);
使用(SqlConnection的CNN1 =新的SqlConnection(cnnString))
{
    使用(CMD1的SqlCommand =新的SqlCommand按钮(Command,CNN1))
    {
        cmd1.Parameters.AddWithValue(@开始时间,SqlDbType.DateTime).value的= System.DateTime.Now;
        cmd1.Parameters.AddWithValue(@ Work_Order,SqlDbType.Int).value的= e.CommandArgument;
        cnn1.Open();
        Label1.Text = cmd1.ExecuteScalar()的ToString()。
        cnn1.Close();
    }
}VAR命令=UPDATE [时间] SET [停止时间] = @stoptime WHERE [PrimaryKey的] = @PrimaryKey;
使用(SqlConnection的CNN =新的SqlConnection(cnnString))
{
    使用(CMD的SqlCommand =新的SqlCommand(命令,美国有线电视新闻网))
    {
        cmd.Parameters.AddWithValue(@停止时间,SqlDbType.DateTime).value的= System.DateTime.Now;
        cmd.Parameters.AddWithValue(@的PrimaryKey,*从INSERT输出的PrimaryKey *        cnn.Open();
        cmd.ExecuteNonQuery();
    }
}


解决方案

而不是让它去标签有它去一个int,然后设置与INT标签文本。然后,通过对第二部分中的int。声明using语句的范围之外INT虽然否则它将被处置,当你尝试,你会得到一个空引用异常以后调用它。

编辑:要添加,这将是更好,如果你转换为存储的特效和定义的SqlParameter对象(你没有他们,你需要他们)。

的SqlParameter

  INT myPK;
    VAR命令1 =INSERT INTO [时间]([开始时间],[工作订单])输出INSERTED.PrimaryKey VALUES(@starttime,@Work_Order);
    使用(SqlConnection的CNN1 =新的SqlConnection(cnnString))
    {
        使用(CMD1的SqlCommand =新的SqlCommand按钮(Command,CNN1))
        {
            cmd1.Parameters.AddWithValue(@开始时间,SqlDbType.DateTime).value的= System.DateTime.Now;
            cmd1.Parameters.AddWithValue(@ Work_Order,SqlDbType.Int).value的= e.CommandArgument;
            cnn1.Open();
            myPk = Convert.ToInt32(cmd1.ExecuteScalar());
            Label1.Text = myPk.ToString();
            cnn1.Close();
        }
    }    VAR命令=UPDATE [时间] SET [停止时间] = @stoptime WHERE [PrimaryKey的] = @PrimaryKey;
    使用(SqlConnection的CNN =新的SqlConnection(cnnString))
    {
        使用(CMD的SqlCommand =新的SqlCommand(命令,美国有线电视新闻网))
        {
            cmd.Parameters.AddWithValue(@停止时间,SqlDbType.DateTime).value的= System.DateTime.Now;
            cmd.Parameters.AddWithValue(@的PrimaryKey,myPK);            的FindControl(Work_OrderLabel); ;            cnn.Open();
            cmd.ExecuteNonQuery();
        }
    }

I have two buttons on a page, one that logs a start time, and one that logs an end time.

The start time button performs an sql insert. At that point i need to grab the primary key that's create. To do this i want to use the sql command (output inserted).

Then when the stop time is clicked,the row should be update with a stop time using the primary key from the start in the where clause. I believe insert SQL is correct but i don't know how to pass the primary key to the next command.

Code dump, with what i have so far.

var command1 = "INSERT INTO [Time] ([Start Time], [Work Order]) OUTPUT INSERTED.PrimaryKey VALUES (@StartTime, @Work_Order)";
using (SqlConnection cnn1 = new SqlConnection(cnnString))
{
    using (SqlCommand cmd1 = new SqlCommand(command1, cnn1))
    {
        cmd1.Parameters.AddWithValue("@StartTime", SqlDbType.DateTime).Value = System.DateTime.Now;
        cmd1.Parameters.AddWithValue("@Work_Order", SqlDbType.Int).Value = e.CommandArgument;
        cnn1.Open();
        Label1.Text = cmd1.ExecuteScalar().ToString();
        cnn1.Close();
    }
}

var command = "UPDATE [Time] SET [Stop Time] = @StopTime WHERE [PrimaryKey] = @PrimaryKey";
using (SqlConnection cnn = new SqlConnection(cnnString))
{
    using (SqlCommand cmd = new SqlCommand(command, cnn))
    {
        cmd.Parameters.AddWithValue("@StopTime", SqlDbType.DateTime).Value = System.DateTime.Now;
        cmd.Parameters.AddWithValue("@PrimaryKey", *PrimaryKey from INSERT output*

        cnn.Open();
        cmd.ExecuteNonQuery();
    }
}

解决方案

instead of having it go to the label have it go to an int and then set the label text with the int. Then pass the int on the second part. Declare the int outside the scope of the using statements though or it will be disposed and you will get a null reference exception when you try and call it later.

Edit: To add, this would be better if you convert to stored procs and define the SqlParameter objects (you don't have them, you'll need them).

SqlParameter

    int myPK;
    var command1 = "INSERT INTO [Time] ([Start Time], [Work Order]) OUTPUT INSERTED.PrimaryKey VALUES (@StartTime, @Work_Order)";
    using (SqlConnection cnn1 = new SqlConnection(cnnString))
    {
        using (SqlCommand cmd1 = new SqlCommand(command1, cnn1))
        {
            cmd1.Parameters.AddWithValue("@StartTime", SqlDbType.DateTime).Value = System.DateTime.Now;
            cmd1.Parameters.AddWithValue("@Work_Order", SqlDbType.Int).Value = e.CommandArgument;
            cnn1.Open();
            myPk = Convert.ToInt32(cmd1.ExecuteScalar());
            Label1.Text = myPk.ToString();
            cnn1.Close();
        }
    }

    var command = "UPDATE [Time] SET [Stop Time] = @StopTime WHERE [PrimaryKey] = @PrimaryKey";
    using (SqlConnection cnn = new SqlConnection(cnnString))
    {
        using (SqlCommand cmd = new SqlCommand(command, cnn))
        {
            cmd.Parameters.AddWithValue("@StopTime", SqlDbType.DateTime).Value = System.DateTime.Now;
            cmd.Parameters.AddWithValue("@PrimaryKey", myPK);

            FindControl("Work_OrderLabel"); ;

            cnn.Open();
            cmd.ExecuteNonQuery();
        }
    }

这篇关于SQL插入输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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