Database Reference
In-Depth Information
Modify the GetConString method as shown in the following snippet to take a
parameter. The parameter lets you specify the connection type so you can
return the correct type of connection string (either ADO.NET or ODBC). Be
sure to use your correct password and server name with the correct server. If
the value of ADO_NET is passed into this method, the ADO.NET connection
string is returned; otherwise the ODBC connection string is returned:
enum ConnType
{
ADO_NET = 1,
ODBC = 2
}
string GetConString(ConnType connType)
{
if (connType == ConnType.ADO_NET)
return "Server=tcp: servername .database.windows.net;Database=TechBio;
User ID=SQLScott@ servername ;Password= password ;
Trusted_Connection=False;Encrypt=True;";
else
return "Driver={SQL Server Native Client
10.0};Server=tcp:servername.database.windows.net;
Database=TechBio;Uid=SQLScott@servername;Pwd=password;Encrypt=yes;";
}
2.
Place a second button on the form, along with a DataGridView. In its click
event, add the following code. This code is just like the code from the
ADO.NET example, but it uses the Odbc data classes versus the Sql data classes.
For clarity, change the Text property of this new button to “ODBC” so you
know the difference between this button and the first button. Notice in the
code that the value “ODBC” is passed in the GetConString method, returning
the ODBC connection string:
string connStr = GetConString(ConnType.ODBC);
3.
using (OdbcConnection conn = new OdbcConnection(connStr))
{
try
{
conn.Open();
OdbcDataAdapter da = new OdbcDataAdapter();
OdbcCommand cmd = new OdbcCommand("SELECT Name FROM Users", conn);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
DataSet ds = new DataSet("Users");
da.Fill(ds);
listBox1.DataSource = ds.Tables[0];
dataGridView1.DataSource = ds.Tables[0];
listBox1.DisplayMember = "Name";
}
catch (OdbcException ex)
{
MessageBox.Show(ex.Message.ToString());
Search WWH ::




Custom Search