跨项目共享SqlConnection [英] Sharing SqlConnection across a project

查看:77
本文介绍了跨项目共享SqlConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以每种格式我都有一些代码可以连接到SQL数据库...

So I have a bit of code in each form to connected to SQL databases...

private SqlConnection connectLink(string selectDbase)
        {
            SqlConnection returnCon = null;
            try
            {
                returnCon = new SqlConnection("Data Source=" + Properties.Settings.Default.sqlServer + ";"
                        + "Initial Catalog=" + selectDbase + ";"
                        + "Persist Security Info=True;"
                        + "User ID=" + Properties.Settings.Default.dBaseUname + ";Password=" + unScramble(Properties.Settings.Default.dBasePword));
            }
            catch
            {
                MessageBox.Show("There was an error connecting to the " + selectDbase + " Database");
            }
            return returnCon;
        }




每当我想连接到数据库时,都会调用该名称...




Which I then call whenever I want to connect to a database...

SqlConnection getDetails = connectLink(Properties.Settings.Default.linkDbase);



可能是一个愚蠢的问题,但是无论如何,我可以将其放在单独的.cs文件中,而不是放在每个表单代码中吗?我对C#很陌生...



probably a dumb question but is there anyway I can have this sitting in a separate .cs file rather than in each forms code? I''m pretty new with C#...

推荐答案

请参阅本文
用于从DataBase和使用反射将DataTable保存到数据库中 [
Please see this article
General purpose class to fill DataTable(s) from DataBase and to save DataTable(s) to DataBase using reflection[^]
After instantiating the DataBaseConnector class with connection string it can be used at several places, to fill and save the data in DataTables


Microsoft为此目的创建了一个名为SqlHelper的类.源代码在这里...

http://www.koders.com/csharp/fidD4121D6E4BCA2DAB656D770903FECBFF7427D242.aspx /[
Microsoft created a class for exactly this purpose, named SqlHelper. The source code is here...

http://www.koders.com/csharp/fidD4121D6E4BCA2DAB656D770903FECBFF7427D242.aspx[^]

You don''t have to keep creating SQL Connections with this class, you can just send it your connection string to one of the overloads. e.g.

string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"

// Get a dataset from a Straight text command
System.Data.DataSet results = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteDataset(connectionString, System.Data.CommandType.Text, "SELECT Field1 FROM Table5");


// Get a data reader from a stored procedure with parameters
int someParameter1 = 5;
string someParameter2 = "Test";
using (var reader = Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(connectionString, "MyStoredProcedureName", someParameter1, someParameter2))
{
    while (reader.Read())
    {
        // do something
    }
}


请参阅参考资料.
http://www.dotnetfunda.com/articles/article71.aspx [
see reference.
http://www.dotnetfunda.com/articles/article71.aspx[^]


这篇关于跨项目共享SqlConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆