SQL数据库链接C#的Application

来源:互联网 发布:淘宝儿童摇摆车扭扭车 编辑:程序博客网 时间:2024/06/05 18:07

对于向工程中添加数据库和数据库的链接条件语句,这边就不多说了

链接语句是系统自动生成的,添加一个String变量储存即可

下面是关于主程序的代码,留作备用

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data.Sql;using System.Data.SqlClient;namespace DataBaseTest{    class Program    {        static void Main(string[] args)        {            try            {                SqlConnection connection = new SqlConnection();                connection.ConnectionString = "Data Source=(local);Initial Catalog=Students;Integrated Security=True";                connection.Open();                SqlCommand command = new SqlCommand();                command.Connection = connection;                command.CommandText = "USE Students SELECT * FROM StuInformation";                SqlDataReader reader = command.ExecuteReader();                while (reader.Read())                {                    int stuId = reader.GetInt32(0);                    string stuName = reader.GetString(1);                    string stuSex = reader.GetString(2);                    Console.WriteLine("ID "+stuId + " 姓名  " + stuName +"性别 " + stuSex);                }            }            catch(Exception ex)            {                Console.WriteLine(ex.Message);            }            //System.Console.WriteLine("123");        }    }}


 

0 0