【问题标题】:How to managed two sql queries in ASP.Net (Visual Studio 2010)如何在 ASP.Net (Visual Studio 2010) 中管理两个 sql 查询
【发布时间】:2018-04-10 18:14:39
【问题描述】:

所以我想要做的是点击一个按钮。我希望一个 sql 查询将值插入“Return_Process”表,另一个 sql 查询从另一个表中的匹配贷款 ID 中删除数据,即“Loan_Process”。

这是我编写的代码,但它没有删除任何内容,它将值插入到返回过程中,但没有从贷款过程中删除它。

    //Global variable declaration
    string path;
    string sql;
    string sql2;
    //create a method for database connection
    public void connection()
    {
        //connection string
        path = @"Data Source=NATHAN-PC\SQLEXPRESS;Initial Catalog=ASP;Integrated Security=True";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        {
            connection();

            SqlConnection con = new SqlConnection(path);
            con.Open();

            //try
            {
                sql = "INSERT INTO Return_Process (Return_ID, FIne, Actual_Returned_Date, Loan_ID) VALUES ('" + txtRID.Text + "','" + txtfine.Text + "','" + TextBox1.Text + "','" + txtLID.Text + "')";
                sql2 = "Delete FROM Loan_Process WHERE Loan_ID='"+txtLID+"'";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();
                //lblerrormsg.Visible = true;
                //lblerrormsg.Text = "Success";
                con.Close();
                //GridView1.DataBind();
            }

            //catch (SqlException)
            //{
            //    //lblerrormsg.Visible = true;
            //    //lblerrormsg.Text = "Invalid";
            //}


            con.Close();
            //GridView1.DataBind();

        }

    }
}

}

我的 ASP.net 很差,所以如果有人能告诉我如何同时执行这两个查询,将不胜感激。

【问题讨论】:

  • 将两个查询合二为一(以; 分隔)。在查询中也使用@parameters
  • 我很抱歉,就像我在 asp.net 上说的我很糟糕只是为我的课程分配了一个项目,但不知道如何做到这一点。所以像 sql = "query1" ; “查询 2”?像这样还是?

标签: asp.net sql-server visual-studio-2010


【解决方案1】:

做这样的事情:

//your code
sql = "INSERT INTO Return_Process (Return_ID, FIne, Actual_Returned_Date, Loan_ID)"
    + " VALUES (@rid, @fine, @retDate, @lid); " //note ; inside
    + "Delete FROM Loan_Process WHERE Loan_ID=@lid;";
var cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@rid", SqlDbType.Int).Value = Int.Parse(txtRID.Text);
//similar for 3 remaining parameters. Just set correct SqlDbType
con.Open();
cmd.ExecuteNonQuery();
con.Close();

【讨论】:

  • 您好,感谢您的回复。你设置正确的sqldbtype是什么?对于我的代码,它会是什么?很抱歉试图跟上,但我在这方面真的很弱。我输入了你的代码,它说 Int.Parse 和 SqpDbtype.int 在当前上下文中不存在
  • Int 用于整数类型,DateTime 用于日期等。检查您的数据库架构(表定义)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
相关资源
最近更新 更多