Connecting to DB (need a hint)
Im trying to connect to a database and then feed the data into a binding source that then is displayed in a datagridview that i can filter information out of.
with that said. when i run trial querys in excel I get a sql statment like this
SELECT gci_requests.ref_num, gci_requests.summary, gci_requests.open_date, gci_requests.resolve_date, gci_requests.category_name, gci_requests.group_name, gci_requests.status_name, gci_requests.customer_adaccount, gci_requests.customer_lastname, gci_requests.customer_phone, gci_requests.reportedby_adaccount, gci_requests.assignee_adaccount
FROM mdb_rpt.dbo.gci_requests gci_requests
WHERE (gci_requests.category_name Like '%Publishing USAT%')
My question do i use the FROM part in my connection string or is that something else all together?
Code:
System.Data.Odbc.OdbcConnection cn;
System.Data.Odbc.OdbcCommand cmd;
string MyString,
CNstring;
MyString = "Select * from gci_Requests";
CNstring = "Driver={SQL Server};Server=mdb_rpt.dbo.gci_requests;UID=caiahd_rpt;PWD=dashboard;";
//cn = new System.Data.Odbc.OdbcConnection("Driver={SQL Server};Server=" + myServerAddress + ";Database=" + myDataBase + ";Uid=" + myUsername + ";Pwd=" + myPassword + ";");
cn = new System.Data.Odbc.OdbcConnection(CNstring);//Database=CA_ServiceDesk_RPT;
cmd = new System.Data.Odbc.OdbcCommand(MyString, cn);
//
try
{
cn.Open();
CA_bindingsource.DataSource = cmd;
dataGridView1.DataSource = CA_bindingsource;
MessageBox.Show("Connected");
}
Re: Connecting to DB (need a hint)
If you're connecting to SQL Server then I strongly suggest that you use SqlClient rather than Odbc.
That connection string is incorrect. The "Server" part of connection string is the name of the SQL Server instance you want to connect to. If you want to specify a database name too then that goes in a separate property of the connection string. For SqlClient its "Data Source" but I'm not sure for Odbc. The FROM part of your query is specifying the table to retrieve data from. The connection string cares nothing for tables.
Re: Connecting to DB (need a hint)
If you are pulling from one table then you do not need to use the table name with the field name as well. This is not an issue but makes it more easily readable.
Re: Connecting to DB (need a hint)
Re: Connecting to DB (need a hint)
so if the server name was gci-mocsqdb01
would i want to use the
SqlConnection (.NET)
Standard Security
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
And if so what would the catalog be?
1 Attachment(s)
Re: Connecting to DB (need a hint)
The Data Source is the server and instance name. The Initial Catalog is the name of the database.
You're probably getting confused because of the loose terminology people generally use. People will call SQL Server a "database" but it's actually an RDBMS, i.e. a system that manages relational databases. Each installation of SQL Server manages multiple databases.
In the attached image my Data Source would be "Serenity\SQL2000" and my Initial Catalog would be "RMSSample". Serenity is the name of the physical server on which SQL Server is installed and its a named instance called SQL2000. I have two other instances on the same server named SQL2005 and SQLExpress. This instance manages all the databases listed. The user name and password you provide are for the SQL Server instance, then you can connect to any database within that instance.