我正在用 C# 开发一系列重用相同 SQL 代码的进程。下面是我为与我的测试数据库的 SQL 连接创建的类的示例。问题:如何在我的过程中调用类?我尝试了几件事,但是出现以下错误
错误:
SQLHelperCode.FirstConnect is a 'type' which is not valid in the given context.
Only Assignment, call, increment, decrement and new object expressions can be used as a statement
FirstConnect 类
public class FirstConnect
{
public FirstConnect()
{
SqlConnection conn;
string path = @"C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL\Data";
const string dbName = "datadetail";
{
conn = new SqlConnection("user id=TestUser;" +
"server=TestData\\SQLEXPRESS;" +
"Trusted_Connection=yes;" +
"connection timeout=30");
try
{
conn.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
想在这段代码中调用 FirstConnect:
protected override void OnBarUpdate()
{
accountName = Account.Name.ToString();
if (accountName == 'Test1234')
{
//Call FirstConnect here.
}
}