SQL连接方法 [英] SQL Connection Methods
问题描述
Hey Fellow Coders,
我想打开一个对话框,了解社区在这种情况下做了什么。我需要连接到我对此窗口的SQL Server。下面的代码工作正常,但我不喜欢它。我真正想要的是一种在代码中没有连接字符串的情况下建立连接的方法。创建一个文件(.txt,.properties,.settings,.whatever)并从那里读取连接字符串,或创建一个类并一次设置连接字符串(或字符串)。
P.S.我不想要这个:
使用(
var conn =
new SqlConnection( @ Data Source = server_name; Initial Catalog = DB; Integrated Security = True; Persist Security Info = False; Encrypt = True; TrustServerCertificate = True;) )
我宁愿使用资源,参考,类等等。每次我将此代码发布给新人时,我都不想更改此内容。我想我想要一个设置它并忘记它的方法。
P.S。非常重要 - 通过Visual Studio中的ClickOnce释放代码。 因此,无论何种方法都需要使用ClickOnce。
public static void GetPatient()
{
尝试
{
使用(
var conn =
new SqlConnection( @ 数据源= server_name;初始目录= DB;集成Security = True; Persist Security Info = False; Encrypt = True; TrustServerCertificate = True;))
{
var preQuery = Settings.Default.SqlGetPatient;
var queryString = preQuery + @ PatId =' + PatId + ';
conn.Open();
Log.Info( 建立SQL连接并打开与病人数据库的通信: + PatId);
使用( var command = new
SqlCommand(queryString,conn))
{
var reader = command.ExecuteReader();
while (reader.Read())
{
PatName = reader [ 0 ] as string ;
PatId = reader [ 1 ] as string 跨度>;
PatSys =阅读器[ 2 ] as string 跨度>;
PatAge = reader [ 3 ] as string 跨度>;
break ;
}
}
Log.Debug( 注意查找: + queryString);
Log.Debug( DB Connection as: + conn);
conn.Close();
}
}
catch (SqlException ex)
{
// 为异常上传构建自定义消息框
var msg = ex.ToString();
var title = Resources.CustomMsgBxTitle;
MessageBoxManager.Ok = 上传;
MessageBoxManager.Cancel = 忽略;
MessageBoxManager.Register();
var result = MessageBox.Show(msg,title,MessageBoxButtons.OKCancel,MessageBoxIcon.Hand);
switch (result)
{
case DialogResult.OK:
Clipboard.SetText(msg);
Process.Start( http://weburl.com/open.php) ;
break ;
case DialogResult.Cancel:
break ;
}
MessageBoxManager.Unregister();
}
Log.Info( 将值传递给MainWindow:患者ID: + PatId + ,患者姓名: + PatName +
,患者SYS_ID: +
PatSys + ,患者年龄: + PatAge + - END 跨度>);
}
试试这些
如何从C#中的App.Config获取连接字符串 [ ^ ]
连接字符串和配置文件 [ ^ ]
Hey Fellow Coders,
I want to open a dialog to find out what the community does in this type of situation. I need to connect to the SQL Server I have for this Window. The code below works just fine, but I don't like it. What I really want is a way to "make a connection" without a connection string in the code. Either make a file (.txt, .properties, .settings, .whatever) and read the connection string from there, or create a single class and set the connection string (or strings) one time.
P.S. I don't want this:
using (
var conn =
new SqlConnection(@"Data Source=server_name;Initial Catalog=DB;Integrated Security=True;Persist Security Info=False;Encrypt=True;TrustServerCertificate=True;"))
I would rather use a resource, reference, class, whatever. I don't want to change this every time I release this code to someone new. I guess I want a "set it once and forget it" method.
P.S. Very Important - Releasing the code via ClickOnce in Visual Studio. Therefore, whatever method needs to work with ClickOnce.
public static void GetPatient()
{
try
{
using (
var conn =
new SqlConnection(@"Data Source=server_name;Initial Catalog=DB;Integrated Security=True;Persist Security Info=False;Encrypt=True;TrustServerCertificate=True;"))
{
var preQuery = Settings.Default.SqlGetPatient;
var queryString = preQuery + "@PatId = '" + PatId + "'";
conn.Open();
Log.Info("Established a SQL Connection and opened communication to database for patient: " + PatId);
using (var command = new
SqlCommand(queryString, conn))
{
var reader = command.ExecuteReader();
while (reader.Read())
{
PatName = reader[0] as string;
PatId = reader[1] as string;
PatSys = reader[2] as string;
PatAge = reader[3] as string;
break;
}
}
Log.Debug("Note Lookup as: " + queryString);
Log.Debug("DB Connection as:" + conn);
conn.Close();
}
}
catch (SqlException ex)
{
// Build custom message box for exception upload
var msg = ex.ToString();
var title = Resources.CustomMsgBxTitle;
MessageBoxManager.Ok = "Upload";
MessageBoxManager.Cancel = "Ignore";
MessageBoxManager.Register();
var result = MessageBox.Show(msg, title, MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
switch (result)
{
case DialogResult.OK:
Clipboard.SetText(msg);
Process.Start("http://weburl.com/open.php");
break;
case DialogResult.Cancel:
break;
}
MessageBoxManager.Unregister();
}
Log.Info("Passing values to MainWindow: Patient ID: " + PatId + ", Patient Name: " + PatName +
", Patient SYS_ID: " +
PatSys + ", Patient Age: " + PatAge + " - END");
}
Try these
How to get Connection String from App.Config in C#[^]
Connection Strings and Configuration Files[^]
这篇关于SQL连接方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!