| 分類:[.NET 全般]
http://www.atmarkit.co.jp/fdotnet/basics/adonet01/adonet01_03.html このページを参考にして以下のコードを書いたのですが、 sqlDA.Fill(dt); この行で「sqlexceptionはバンドルされませんでした」とでます。 たぶんデーターベースのアドレスの設定に間違いがあると思うのですが、具体的にどうなおせばよいのでしょうか? よろしくお願いします。
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.SqlClient;
namespace WindowsFormsApplication1 { public partial class Form1 : Form //class DBViewer : Form { /*public Form1() { }*/
string databaseName; ListBox lstDB = new ListBox(); ListBox lstTBL = new ListBox(); DataGrid dg = new DataGrid();
public Form1() { InitializeComponent(); this.Width = 800; this.Height = 600; this.Text = "DBViewer";
lstDB.Location = new Point(10, 10); lstDB.Width = 200; lstDB.Height = 200; lstDB.DisplayMember = "name"; lstDB.SelectedIndexChanged += new EventHandler(this.OnDatabase);
lstTBL.Location = new Point(220, 10); lstTBL.Width = 200; lstTBL.Height = 200; lstTBL.DisplayMember = "name"; lstTBL.SelectedIndexChanged += new EventHandler(this.OnTable);
dg.Location = new Point(10, 220); dg.Width = 775; dg.Height = 330; dg.RowHeadersVisible = false; dg.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
this.Controls.AddRange(new Control[] { lstDB, lstTBL, dg });
lstDB.DataSource = getTable("master", "select name from sysdatabases"); }
void OnDatabase(Object s, EventArgs e) { databaseName = (string)((DataRowView)lstDB.SelectedItem).Row[0]; lstTBL.DataSource = getTable(databaseName, "select name from sysobjects where xtype = 'U' or xtype = 'S' order by name"); }
void OnTable(Object s, EventArgs e) { string table = (string)((DataRowView)lstTBL.SelectedItem).Row[0]; try { dg.DataSource = getTable(databaseName, "select * from [" + table + "]"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
SqlConnection getConnection(string database) { string conn = "Server=(local)\\NetSDK; Trusted_Connection=yes; database=" + database; return new SqlConnection(conn); }
DataTable getTable(string database, string query) { SqlDataAdapter sqlDA = new SqlDataAdapter(query, getConnection(database));
DataTable dt = new DataTable(); sqlDA.Fill(dt);
return dt; }
private void Form1_Load(object sender, EventArgs e) {
} } }
|