昨天我问了一个关于从外部类文件中获取结果的问题。我现在明白了。我现在需要帮助理解的是如何将字符串从外部类获取到方法中。这是返回一个字符串或整数,我可以用它来给我的系统用户一些关于他的输入是否被接受的反馈。我想知道是否有人可以在这个问题上帮助我。这是外部类:
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 页面的简单字符串。
对此的任何帮助将不胜感激,在此先感谢。