i've searched a lot on google and didn't find my answer how to do that. I've create a server on SQL Server 2008 and named it Antonio-HP, I've created a database inside it and named it Proba1. How can I connect to my database with C# and fill datas from it to dataGridView control...don't bother with SQL query. I know what I have to do with it. Can someone help me?
11 replies to this topic
#1
Posted 18 August 2011 - 03:19 PM
|
|
|
#2
Posted 18 August 2011 - 05:21 PM
I know their a more visual way with drag&drop from the form designer itself, but I don't remember how to do so.
So here the code way
Than you do a
So here the code way
String connectionString = "Data Source=localhost;Initial Catalog=database_Name;User ID=Username;Password=Password"
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("select * from tableName", connection);
command.CommandType = CommandType.Text;
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection);
Than you do a
while (reader.read())And you have your data
#3
Posted 19 August 2011 - 11:56 AM
i'm trying to connect to my own server but there is always an exception here is the code
string connectionString = "Data Source=ANTONIO-HP;Initial Catalog=Proba1;";
try
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
MessageBox.Show("Veza je uspješno ostvarena");
connection.Close();
}
catch (Exception)
{
MessageBox.Show("Nije ostvarena veza");
}
#4
Posted 19 August 2011 - 12:02 PM
Can you modify your code like this
And tell us what is the result of the first messagebox
catch (Exception e)
{
MessageBox.Show(e.ToString());
MessageBox.Show("Nije ostvarena veza");
}
And tell us what is the result of the first messagebox
#5
Posted 19 August 2011 - 12:35 PM
#6
Posted 19 August 2011 - 12:59 PM
You didn't put the user & the password of the database in your connectionString
;User ID=Username;Password=PasswordLook at my first exemple
#7
Posted 19 August 2011 - 02:21 PM
i have included it but exception said that it's wrong username...i put an unsername from SQL Server 2008 but it's still wrong
#8
Posted 19 August 2011 - 02:36 PM
Is it the same error message?
If so, have you tried using an IP insted of ANTONIO-HP
Is ANTONIO-HP your localhost or a other computer, if so try to desactivate every firewall
If so, have you tried using an IP insted of ANTONIO-HP
Is ANTONIO-HP your localhost or a other computer, if so try to desactivate every firewall
#9
Posted 19 August 2011 - 02:48 PM
#10
Posted 20 August 2011 - 05:19 AM
this is the exception when I'm using IP instead server name
ImageShack® - Online Photo and Video Hosting
ImageShack® - Online Photo and Video Hosting
#11
Posted 20 August 2011 - 12:21 PM
I made it. I've changed connection string into
Now how to fill data from database into dataGridView control. Can someone give me an example for 2 columns
"Persist Security Info=False;Integrated Security=true;Initial Catalog=Proba1;server=(local)"Now there is no exception during opening connection to my database
Now how to fill data from database into dataGridView control. Can someone give me an example for 2 columns
#12
Posted 21 August 2011 - 03:17 AM
i found some example on google but it seems it's not working properly
this is the code:
this is the code:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Database1
{
public partial class Form1 : Form
{
SqlCommand sCommand;
SqlDataAdapter sAdapter;
SqlCommandBuilder sBuilder;
DataSet sDs;
DataTable sTable;
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string connectionString = "Persist Security Info=False;Integrated Security=true;Initial Catalog=Proba1;server=(local)";
try
{
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
MessageBox.Show("Veza je uspješno ostvarena");
string sql = "SELECT first, last FROM employee";
sCommand = new SqlCommand(sql, conn);
sAdapter = new SqlDataAdapter(sCommand);
sBuilder = new SqlCommandBuilder(sAdapter);
sDs = new DataSet();
sAdapter.Fill(sDs, "employee");
sTable = sDs.Tables["employee"];
conn.Close();
dataGridView1.DataSource = sDs.Tables["employee"];
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
MessageBox.Show("Nije ostvarena veza");
}
}
}
}
debugger don't give any error or warning but dataGridView doesn't show any data...why???
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









