昨天我问了一个关于从外部类文件中获取结果的问题。我现在明白了。我现在需要帮助理解的是如何从外部类中获取字符串到方法中。这是为了返回一个字符串或整数,我可以用来给我的系统用户一些关于他的输入是否被接受的反馈。我想知道是否有人可以在这个问题上帮助我。这是外部课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// This code file saves title information. It is used by both the addTitleFromLansweeper and addTitleManually pages.
/// </summary>
public class AddTitle
{
public static void ExecuteInsert(string name, string type, string total)
{
int rows = 0;
//Creates a new connection using the HWM string.
using (SqlConnection HWM = new SqlConnection(Connections.ConnectionHWM()))
{
//Create a Sql String that will only execute when the combination of values in the entry form are unique.
string sql = " IF NOT EXISTS "
+ " ( SELECT 1 "
+ " FROM tblSoftwareTitles "
+ " WHERE Softwarename = @SoftwareName "
+ " AND SoftwareSystemType = @Softwaretype "
+ " ) "
+ " BEGIN "
+ " INSERT tblSoftwareTitles (SoftwareName, SoftwareSystemType, TotalLicences) "
+ " VALUES (@SoftwareName, @SoftwareType, @Licences); "
+ " END ; ";
//Open the connection.
HWM.Open();
try
{
//Create an Sql command.
using (SqlCommand addSoftware = new SqlCommand
{
CommandType = CommandType.Text,
Connection = HWM,
CommandTimeout = 300,
CommandText = sql
})
{
//Add parameters to the Sql command.
addSoftware.Parameters.Add("@SoftwareName", SqlDbType.NVarChar, 200).Value = name;
addSoftware.Parameters.Add("@SoftwareType", SqlDbType.Int).Value = type;
addSoftware.Parameters.Add("@Licences", SqlDbType.Int).Value = total;
try
{
//Execute the query.
rows = addSoftware.ExecuteNonQuery();
if (rows >= 1)
{
//If a row is added then show a success message box.
Alert.Show("Software has been saved!");
}
else
{
//If a row isn't added then show a failure message box.
Alert.Show("Software title already exists");
}
}
catch (System.Data.SqlClient.SqlException ex)
{
//If there is a problem with execution then show a Message with the exception
Alert.Show(ex.Message);
}
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
}
}
}
我知道私有静态void不会返回结果。我一直在寻找一种方法来将它作为私有静态字符串,声明一个包含在我的警告框中的消息的字符串。然后,代码隐藏文件使用该方法,获取该字符串方法,然后显示警报。
我是否在追逐一个松散的结局?我一直在使用using语句中使用的类型必须可以隐式转换为'system.idisposable'。这个方法适用于数据集但不能使用我希望传递回我的asp页面的简单字符串。
在此之前,非常感谢任何帮助。