[已解决]在静态函数上使用全局变量的问题 [英] [SOLVED] Problem using global variables at static functions

查看:278
本文介绍了[已解决]在静态函数上使用全局变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常新手的问题...所以让我们看看:

我定义了全局变量:

I know this a very newbie question... so lets see:

I define global variables:

string strConn;
     SqlCommand cmd;
     SqlConnection conn;




然后在page_load上:




and then on page_load:

strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
       conn = new System.Data.SqlClient.SqlConnection(strConn);
        conn.Open();



问题出在我不能做的公共静态功能上:



The problem is with public static functions I cant do:

cmd = new SqlCommand("sql query..", conn);



因为我得到了:at(cmd和conn)

非静态字段,方法或属性需要对象引用

我该如何解决这个问题?

已解决:

是的,我知道这是一个新手问题(尽管我需要一个多小时才能知道该怎么做).



because I get: at (cmd and conn)

An object reference is required for the nonstatic field, method, or property

how can I solve this problem ?

SOLVED:

Yes I know it was a newbie question (altough I need more than one hour to know how to do it).

public static SqlCommand cmd;
     public static SqlConnection conn;

推荐答案

仍然跌跌撞撞,我看到:rolleyes:

如果您已经读过一本书,那么在ADO.NET上有章节的任何书籍,您都将了解如何实现.我会给你一个提示;它不涉及全局变量,并且确实使用了using方法.
Still stumbling around I see :rolleyes:

If you had picked up a book, any book that has a chapter on ADO.NET, you would see how to do this. I''ll give you a hint; it doesn''t involve global variables and does make use of the using method.


这篇关于[已解决]在静态函数上使用全局变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆