【牛腩新闻发布系统】静态方法和非静态方法的调用
2016-10-09 22:35
351 查看
非静态方法
非静态方法在调用时需要new一个实例。namespace DAL { public class SQLHelper { public int test() //非静态方法 { //数据库连接字符串 string connStr = @"Server=JokerLiu\SQLEXPRESS;Database=newssystem;User ID=sa;Password=1"; //创建数据库连接对象,传入数据库连接字符串 SqlConnection conn = new SqlConnection(connStr); //打开数据库连接 conn.Open(); //SQL语句 string sql = "insert into T_category(name) values('测试')"; //创建command执行对象 SqlCommand cmd = new SqlCommand(sql, conn); //接收返回值 int result = cmd.ExecuteNonQuery(); //关闭数据库连接 conn.Close(); return result; } }
}
namespace Web { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write (new SQLHelper ().test ()); //非静态方法的实例化 } } }
静态方法
静态方法直接调用,但是需要初始化。namespace DAL { public class SQLHelper { public static int test() //静态方法 { //数据库连接字符串 string connStr = @"Server=JokerLiu\SQLEXPRESS;Database=newssystem;User ID=sa;Password=1"; //创建数据库连接对象,传入数据库连接字符串 SqlConnection conn = new SqlConnection(connStr); //打开数据库连接 conn.Open(); //SQL语句 string sql = "insert into T_category(name) values('测试')"; //创建command执行对象 SqlCommand cmd = new SqlCommand(sql, conn); //接收返回值 int result = cmd.ExecuteNonQuery(); //关闭数据库连接 conn.Close(); return result; } } }
namespace Web { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write (DAL.SQLHelper.test()); //非静态方法的实例化 } } }
参考链接
静态方法与非静态方法的区别JAVA的静态变量、静态方法、静态类
感谢您的宝贵时间,祝生活愉快,谢谢~
——joker
相关文章推荐
- 牛腩新闻发布系统——主外键关系的设置方法
- 【牛腩新闻发布系统】真假分页、动态网页与静态网页、同步与异步定义总结
- 新闻发布系统——静态方法使用问题(Static)
- 牛腩新闻发布系统笔记9:存储过程
- 牛腩新闻发布系统综合笔记(三)
- 牛腩新闻发布系统总结
- 牛腩新闻发布系统——触发器使用
- 牛腩新闻发布系统 (3) 存过过程或函数""需要""参数,但未提供该参数
- 牛腩新闻发布系统概况
- 牛腩新闻发布系统笔记10:再次重构SQLHelper
- 牛腩新闻发布系统总结
- 牛腩新闻发布系统 aspnetpager分页(使用Repeater 分页,使用ajax刷新) DropDownList 插入一个item
- 牛腩新闻发布系统——后台登录界面生成验证码
- 牛腩新闻发布系统笔记4:重构SQLHelper
- 牛腩新闻发布系统---总体概况
- 1月份看牛腩的视频写的新闻发布系统,在此谢谢牛腩老师
- 牛腩新闻发布系统综合笔记(二)
- 牛腩新闻发布系统笔记7:类别表操作类的实现
- 牛腩新闻发布系统 (3) 存过过程或函数""需要""参数,但未提供该参数
- 牛腩新闻发布系统"小插曲"------"项目文件""已被重命名或已不在解决方案中"