English 中文(简体)
C# C # 中的呼叫类
原标题:Calling Class in C#
  • 时间:2012-05-25 16:57:56
  •  标签:
  • c#
  • sql
  • class

我在C# 中开发一系列程序, 重复使用相同的 SQL 代码 。 下面是为 SQL 连接到测试数据库的 SQL 连接而创建的 类 I 的例子。 问题: 如何在我的进程中给该类打电话? 我尝试了几件事, 但是, 我得到了以下错误 。

错误 :

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

类 一级 Connect

public class FirstConnect
{
    public FirstConnect()
    {
        SqlConnection conn;
        string path = @"C:Program Files (x86)Microsoft SQL ServerMSSQL.1MSSQLData";
        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());
            }
        }
    }
}

想要在这个代码中调用第一个Connect:

protected override void OnBarUpdate()
{
    accountName = Account.Name.ToString();

    if (accountName ==  Test1234 )
    {
        //Call FirstConnect here.
    }
}
问题回答

此直线定义类

public class FirstConnect
 {

此直线定义了 constituter

    public FirstConnect()
      {

以下将定义类型为 FirstConnect 的变量, 然后调用构建器来创建它( 我让两行明确)

FirstConnect fc;
fc  = new FirstConnect();

通常,您会希望有一个实际对对象执行操作的方法

例如

SomeOtherObject varaibleName = fc.GetSomeData(accountName);

FirstConnect fc=新的FirstConnect()

不是答案,只是一个大评论。。。

当使用实现IDisposable的类时,就像SqlConnection

using (var conn = new SqlConnection("user id=TestUser;server=TestData\SQLEXPRESS;Trusted_Connection=yes;connection timeout=30"))
{
   //... do work ... 
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...