TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Anna Rita
NA
265
58.6k
Visual c# visualizzare tabelle dentro un datagridView
Jun 19 2014 3:34 PM
Ciao a tutti,
tramite il controllo OpenFileDialog in un Windows Form, sto cercando di aprire delle tabelle e renderle visibili dentro un dataGridView, ma continua a darmi errori.
Qualcuno sa aiutarmi???
Come Database ho preso come esempio Northwind in Sql Server 2008R2, anche xkè poi dovrò farlo con quello dell'azienda e stavo facendo delle prove, ma nessun risultato....
Vi posto il mio codice, ok?
[\CODE]
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection MyConn = new SqlConnection();
DialogResult dr;
dr = openFileDialog1.ShowDialog();
if (dr == DialogResult.OK)
{
string strFile = openFileDialog1.FileName;
MyConn.ConnectionString = "provider= System.Data.SqlClient" +
"System.Data.SqlClient;Data Source =MATY-16-02-2008\\SQLEXPRESS"
+ strFile + ";";
MyConn.Open();
SqlCommand MyCMD = new SqlCommand();
MyCMD.CommandText = "Select Nome CategoryID," + "CategoryName," +
"Description," + "Picture" + "from Categories";
MyCMD.Connection = MyConn;
SqlDataAdapter MyDataAdapter = new SqlDataAdapter();
MyDataAdapter.SelectCommand = MyCMD;
DataSet ds = new DataSet();
ds.Clear();
MyDataAdapter.Fill(ds, "Categories");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Categories";
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog () == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
//preparo la lista di campi
List<string> ListaCampi = new List<string>();
//Ricava il nome della tabella
string Tbl = textBox1.Text;
string[] Table = Tbl.Split('\\');
int numPos = Table.Length;
string TblNameDB = Table[numPos - 1];
string TblName = TblNameDB.Substring(0, TblNameDB.Length - 3);
string QUERY = "";
switch (TblName.ToUpper())
{
case "Categories":
QUERY = "Select * From Categories";
break;
//default:
}
SqlDataAdapter da = new SqlDataAdapter(QUERY,
ConfigurationSettings.AppSettings["ConnString"]);
SqlCommand MyCMD = new SqlCommand();
MyCMD.CommandText = "select * from TableNameDB";
SqlConnection MyConn = new SqlConnection();
MyCMD.Connection = MyConn;
MyConn.Close();
DataTable ds = new DataTable();
da.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.AutoResizeColumns();
}
}
}
[\CODE]
Reply
Answers (
0
)
referring a library
Multithreading in WebService