I have the following code in my aspx code-behind and it works fine. Because I am going to use it on more than one page, I'd like to move it to a class file. I am not sure how to do this properly.
我的aspx代码中有以下代码,它可以正常工作。因为我要在不止一个页面上使用它,所以我想把它移动到一个类文件中。我不知道该如何正确地做这件事。
#region autocomplete search
[WebMethodAttribute(), ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
SqlConnection con;
SqlCommand cmd;
string cmdString = "SELECT TOP(15) Title FROM posts WHERE (Title LIKE '%" + prefixText + "%') OR (Content LIKE '%" + prefixText + "%')";
con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbMyCMSConnectionString"].ConnectionString);
cmd = new SqlCommand(cmdString, con);
con.Open();
SqlDataReader myReader;
List<string> returnData = new List<string>();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader["Title"].ToString());
}
myReader.Close();
con.Close();
return returnData.ToArray();
}
#endregion
I tried calling it from the code-behind page like this:
我试着从代码背后的页面调用它:
BlogFrontUtil.GetCompletionList(prefixText, count, contextKey);
BlogFrontUtil。GetCompletionList(prefixText计数,contextKey);
...but it's not working, I get the red squiggly lines. The error message says that it is a method but used like a type.
…但它不管用,我得到了红色的曲线。错误消息说,它是一种方法,但使用的是类型。
Could some one please teach me how this is done properly. I have limited experience.
有人能教我如何正确地做这件事吗?我有经验有限。
Thank you
谢谢你!
1 个解决方案
#1
2
In your new class add the method:
在您的新类中添加方法:
public static class NewClass
{
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
SqlConnection con;
SqlCommand cmd;
string cmdString = "SELECT TOP(15) Title FROM posts WHERE (Title LIKE '%" + prefixText + "%') OR (Content LIKE '%" + prefixText + "%')";
con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbMyCMSConnectionString"].ConnectionString);
cmd = new SqlCommand(cmdString, con);
con.Open();
SqlDataReader myReader;
List<string> returnData = new List<string>();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader["Title"].ToString());
}
myReader.Close();
con.Close();
return returnData.ToArray();
}
}
Then in your service call that method:
然后在你的服务中调用这个方法:
[WebMethodAttribute(), ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
string[] s = NewClass.GetCompletionList(prefixText, count, contectKey);
return s;
}
#1
2
In your new class add the method:
在您的新类中添加方法:
public static class NewClass
{
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
SqlConnection con;
SqlCommand cmd;
string cmdString = "SELECT TOP(15) Title FROM posts WHERE (Title LIKE '%" + prefixText + "%') OR (Content LIKE '%" + prefixText + "%')";
con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbMyCMSConnectionString"].ConnectionString);
cmd = new SqlCommand(cmdString, con);
con.Open();
SqlDataReader myReader;
List<string> returnData = new List<string>();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader["Title"].ToString());
}
myReader.Close();
con.Close();
return returnData.ToArray();
}
}
Then in your service call that method:
然后在你的服务中调用这个方法:
[WebMethodAttribute(), ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
string[] s = NewClass.GetCompletionList(prefixText, count, contectKey);
return s;
}