How to use Universal Link Data (C#)?
-
Hallo I try to write a window application that connect to database with udl. The connection is O.k (I tested it), but when I want to use it in my code, it failed. my code: static SqlConnection con = new SqlConnection(); con.ConnectionString = "File Name=C:\\WINNT\\myConnection.udl;"; con.Open(); I also tried: con.ConnectionString = @"File Name=C:\WINNT\PrePaidDb.udl;"; and also try to define it in app.setting. the error is always: {"Keyword not supported: 'file name'." } Do someone has any idea. Thanks a lot
-
Hallo I try to write a window application that connect to database with udl. The connection is O.k (I tested it), but when I want to use it in my code, it failed. my code: static SqlConnection con = new SqlConnection(); con.ConnectionString = "File Name=C:\\WINNT\\myConnection.udl;"; con.Open(); I also tried: con.ConnectionString = @"File Name=C:\WINNT\PrePaidDb.udl;"; and also try to define it in app.setting. the error is always: {"Keyword not supported: 'file name'." } Do someone has any idea. Thanks a lot
The
SqlConnection
class doesn't support the File Name connection string parameter, thus it would fail. You can do this with theOleDbConnection
class however:OleDbConnection conn = new OleDbConnection(@"File Name = c:\test.udl");
try
{
conn.Open();
if(conn.State == ConnectionState.Open)
{
MessageBox.Show("Connection successful.");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}- Nick Parker
My Blog | My Articles -
The
SqlConnection
class doesn't support the File Name connection string parameter, thus it would fail. You can do this with theOleDbConnection
class however:OleDbConnection conn = new OleDbConnection(@"File Name = c:\test.udl");
try
{
conn.Open();
if(conn.State == ConnectionState.Open)
{
MessageBox.Show("Connection successful.");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}- Nick Parker
My Blog | My Articles -
Thank you Nick. The DB is in SqlServer and not in Access, is that means that I couldn't use udl to create connection?
I believe you can use OleDBConnection for SQL also but its not optimized. Why dont you just store the connection in the config file?? :confused: This posting is provided "AS IS" with no warranties, and confers no rights. Alex Korchemniy
-
I believe you can use OleDBConnection for SQL also but its not optimized. Why dont you just store the connection in the config file?? :confused: This posting is provided "AS IS" with no warranties, and confers no rights. Alex Korchemniy
Thank you everybody. Finally I found a way to do what I want. I read the connectionString as a text file.:-O using (StreamReader sr = new StreamReader("c://WINNT//myConnection.udl")) { string line; //--the connection string is define at the third line of the file line = sr.ReadLine(); line = sr.ReadLine(); line = sr.ReadLine(); connectionString = line; }