首页 > Linux操作系统 > Linux操作系统 > ADO.NET连接数据库
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace db_test
{
class Program
{
static void Main(string[] args)
{
SqlConnection userConnection = new SqlConnection("server=ZLD-PC\\SQLEXPRESS;user id=sa;initial catalog=zld119;password=123");
try{userConnection.Open();}
catch{MessageBox.show("打开失败");}
SqlCommand userCommand = new SqlCommand("getUserInfo",userConnection);//getUserInfo为自已写的存储过程
SqlDataReader userDataReader = userCommand.ExecuteReader();
while (userDataReader.Read())
{
Console.WriteLine(userDataReader["Sname"].ToString());
}
userDataReader.Close();
userConnection.Close();
}
}
}
附:getUserInfo存储过程
create procedure getUserInfo
as
select Sname
from student
where Sdept='cs'
GO
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-545469/ ,如需转载,请注明出处,否则将追究法律责任。