网上搜索SQL Server的连接字符串,发现非常多,可以勇哥试了几个都不行。
后来发现用VS本身的数据库连接向导可以方便查看连接字符串。
详细看下面的图片:
下面看一个演示例程:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace masterControl { public partial class Form1 : Form { string DataSource = string.Empty; System.Data.SqlClient.SqlConnection con; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DataSource = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", "ALLEGRO-PC", "guangyu", "sa","mypassword"); try { con=SqlHelper.GetSqlConnection(DataSource); MessageBox.Show("连接成功!"); } catch(Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { var set=SqlHelper.ExecuteDataset(con, CommandType.Text, "select * from user_id"); dataGridView1.DataSource = set.Tables[0]; } } }
其中SqlHelper类是netMarketing类库中的一个类。
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


