我正在 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 语句来说明服务器是否添加记录,然后显示消息框以保存,否则显示消息框,说明数据已经存在。
任何人有任何想法如何实现这一目标?