摘要:本文将带你了解C#编程之 数据库连接 Demo,希望本文对大家学C#/.Net有所帮助。
c#数据库操作Demo
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;using System.Data;
namespace ConsoleApplication1
{
class Program
{ //数据库执行操作类
private SqlCommand cmd = null;
//数据库连接操作类
private SqlConnection mycon = null;
//连接参数
private string conn = "Server=localhost ;Database=IntelligentCity_2015_GZ ;user id=sa;password=123456";
//连接sql
private string sql = "select * from ASM_ACCOUNT";
static void Main(string[] args)
{
Program pr = new Program();
pr.getConnected();
}
//获取连接
private void getConnected()
{
try
{
mycon = new SqlConnection(conn);
mycon.Open();
//调用操作数据库的方法,读取数据库中的数据
operationDatabase(mycon);
}catch(Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
}finally
{
mycon.Close();
}
}
//数据库操作方法,读取数据
private void operationDatabase(SqlConnection conn )
{
cmd = new SqlCommand( sql,conn );
//cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Console.WriteLine("{0},{1}", reader["ACT_LOGIN_NAME"], reader["ACT_LOGIN_PASSWORD"]);
Console.ReadKey();
}
reader.Close();
}
}
}
以上就介绍了C#.NET的相关知识,希望对C#.NET有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号