【问题标题】:Sql server procedure select as Alert for insert procedureSql server 过程选择作为插入过程的警报
【发布时间】:2018-07-19 09:21:58
【问题描述】:

我有一个表格和两个字段。 它们的值通过 ajax 发送到 webmethod。 myCollection 是包含表中列值的数组,lvl 是第一个字段的值,ddl 是第二个字段的值。 这些值需要通过“InsertObject”过程插入到数据库中。 现在我有一个将值 1 发送到 ajax 的 int 成功,然后我警告数据已成功插入数据库中,如果它是 0,而不是警告它不是。

我在 InsertObject 过程中有一个循环,它应该检查数据库中是否已经存在特定的元素组合,如果存在则返回 select "Record already exists"。

我需要有关此选择的帮助。它应该以某种方式发送到 webmethod 和 ajax,这样我就可以提醒用户他尝试了新的组合并且元素无法保存到数据库中。

有人可以帮忙吗? 提前致谢!

 [WebMethod(EnableSession = true)]
        public static int GetCollection(List<myCollection> omyCollection, int ddl, string lvl)
        {   //Datatable is filled with foreach loop

        DataTable tableMain = new DataTable();
        tableMain.Columns.Add("CharID");
        tableMain.Columns.Add("CharaValue");

        foreach (myCollection mycol in omyCollection)
        {
            string label = mycol.label;
            string opis = mycol.opis;

            tableMain.Rows.Add(label, opis);

        }

        int success = 0;
        string conn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(conn))

            try
            {   
                //Check if values allready exist in the database 

                connection.Open();
                SqlCommand cmdCount = new SqlCommand("getDuplicate", connection);
                cmdCount.CommandType = CommandType.StoredProcedure;
                cmdCount.Parameters.AddWithValue("@ObjectName", lvl);


                var count = (string)cmdCount.ExecuteScalar();



                if (count == null)
                {
                    //Database insert
                    SqlCommand cmdProc = new SqlCommand("InsertObject", connection);
                    cmdProc.CommandType = CommandType.StoredProcedure;

                    //Parameters are sent to the database
                    cmdProc.Parameters.AddWithValue("@ObjectName", lvl);
                    cmdProc.Parameters.AddWithValue("@BCID", ddl);
                    cmdProc.Parameters.AddWithValue("@CharaValueParamether", tableMain);


                    cmdProc.ExecuteNonQuery();

                    //If the values are inserted send value 1 to ajax
                    success = 1;

                }

                else
                {

                }

            }

            catch
            {

            }
            finally
            {
                connection.Close();

            }

        return success;


    }

【问题讨论】:

  • 所以如果记录重复,您想返回错误消息吗?
  • 是的,没错!选择“记录已存在”到 webmethod,然后将它们作为警报发送到 ajax。我想我可以用 ajax 管理,我需要 c# 部分:/

标签: c# sql-server stored-procedures webmethod


【解决方案1】:

你只需要创建一个响应对象

public enum InsertStatus{
success = 0,
failure =1 
}
public class InsertResponse
{
 public InsertStatus status {get;set;}
 public string Message {get;set;}
 }

如果您发现多个条目返回您的响应,只需创建一个具有失败状态的对象并设置消息“记录已存在”,如果您能够在数据库中插入记录,则将状态设置为成功并设置消息“记录插入成功”

例如

     public static InsertResponse GetCollection(List<myCollection> omyCollection, int ddl, string lvl)
            { 
     // code
var count = (string)cmdCount.ExecuteScalar();
   if ( count >1 )
return new InsertResponse(){status =failure,Message = "Record Already Exists"};
// code

// similarly after success
return new InsertResponse(){status =success,Message = "Record Inserted"};
    }

您注意到我已将函数的返回类型更改为插入响应。

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多