0

我正在 Asp.net 中制作数据输入页面。输入现在很好,它现在拥有它需要的所有功能,但我想确认数据已经输入。

最初,虽然在查询成功时使用了一些 javascript 来显示一个消息框,但我还是有了这个。直到我只在输入的数据在数据库中不存在时才更改查询插入,这才有效。

这是用于数据输入的 Asp 方法:

    private void ExecuteInsert(string name, string type)
{
    //Creates a new connection using the HWM string
    using (SqlConnection HWM = new SqlConnection(GetConnectionStringHWM()))
    {
        //Creates a sql string with parameters
        string sql = " IF NOT EXISTS "
                 + " ( SELECT  1 "
                 + " FROM tblSoftwareTitles "
                 + " WHERE Softwarename = @SoftwareName "
                 + " AND SoftwareSystemType = @Softwaretype "
                 + " ) "
                 + " BEGIN "
                 + " INSERT tblSoftwareTitles (SoftwareName, SoftwareSystemType) "
                 + " VALUES (@SoftwareName, @SoftwareType) "
                 + " END; ";

        //Opns the connection
        HWM.Open();
        try
        {
            //Creates a Sql command
            using (SqlCommand addSoftware = new SqlCommand{
                CommandType = CommandType.Text,
                Connection = HWM,
                CommandTimeout = 300,
                CommandText = sql})
            {
                //adds parameters to the Sql command
                addSoftware.Parameters.Add("@SoftwareName", SqlDbType.NVarChar, 200).Value = name;
                addSoftware.Parameters.Add("@SoftwareType", SqlDbType.Int).Value = type;
                //Executes the Sql
                addSoftware.ExecuteNonQuery();
            }

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }

    }
}

和 javascript 调用

 Alert.Show("Software instance saved!");

这只是转到一个单独的文件。

最初,我让消息框脚本出现在 try 语句的末尾。我需要帮助从服务器获取休息信息,这将允许我在数据完全唯一且已保存或数据不唯一且尚未输入时显示消息框。

我基本上需要一个 if 语句来说明服务器是否添加记录,然后显示消息框以保存,否则显示消息框,说明数据已经存在。

任何人有任何想法如何实现这一目标?

4

1 回答 1

0

Ok so I did some research and at first chase up the possibilty of using executeScalar(); as I can return a value where I thought executeNonQuery(); didn't return any at all. Seems I was mistaken and it actually returns the number of rows affected. Once I found this out It was a simple case of modifying my code to do what I needed. Here's working code!

    private void ExecuteInsert(string name, string type)
{
    int rows = 0;
    //Creates a new connection using the HWM string
    using (SqlConnection HWM = new SqlConnection(GetConnectionStringHWM()))
    {
        //Creates a sql string with parameters
        string sql = " IF NOT EXISTS "
                 + " ( SELECT  1 "
                 + " FROM tblSoftwareTitles "
                 + " WHERE Softwarename = @SoftwareName "
                 + " AND SoftwareSystemType = @Softwaretype "
                 + " ) "
                 + " BEGIN "
                 + " INSERT tblSoftwareTitles (SoftwareName, SoftwareSystemType) "
                 + " VALUES (@SoftwareName, @SoftwareType); "
                 + " END ; ";

        //Opns the connection
        HWM.Open();
        try
        {
            //Creates a Sql command
            using (SqlCommand addSoftware = new SqlCommand{
                CommandType = CommandType.Text,
                Connection = HWM,
                CommandTimeout = 300,
                CommandText = sql})
            {
                //adds parameters to the Sql command
                addSoftware.Parameters.Add("@SoftwareName", SqlDbType.NVarChar, 200).Value = name;
                addSoftware.Parameters.Add("@SoftwareType", SqlDbType.Int).Value = type;
                //Executes the Sql

                try
                {
                    rows = addSoftware.ExecuteNonQuery();

                    if (rows >= 1)
                    {
                        Alert.Show("Software Saved");
                    }
                    else
                    {
                        Alert.Show("Software already exists");
                    }
                }
                catch (Exception ex)
                {
                    Alert.Show(ex.Message);
                }
            }

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }

    }
}

And there you have it! If the combination of software type and software name are already in the database a message box will appear telling the user of that fact. If the Combination of name and type aren't present then the information will be saved and a message box will be displayed telling the user of the successful insert.

于 2013-08-01T15:58:15.653 回答