赞
踩
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.Odbc;
-
- namespace DataBaseConnectSqlServer
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void buttonCancel_Click(object sender, EventArgs e)
- {
- this.Close();
- }
-
- private void buttonConnectToSqlServer_Click(object sender, EventArgs e)
- {
- try
- {
- //DSN:mylink数据源的名称 UID:sql server登录时的身份sa PWD:登录时的密码123456
- //生成连接数据库字符串
- string ConStr = "DSN=mylink;UID=sa;PWD=123456";
- //定义SqlConnection对象实例
- OdbcConnection odbcCon = new OdbcConnection(ConStr);
- string SqlStr = "select * from StudentInfo";
-
- OdbcDataAdapter odbcAdapter = new OdbcDataAdapter(SqlStr, odbcCon);
- DataSet ds = new DataSet();
-
- odbcAdapter.Fill(ds);
- this.dataGridView.DataSource = ds.Tables[0].DefaultView;
- }
- catch (System.Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。