连接字符串 [英] Conncection string

查看:89
本文介绍了连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,请让我知道如何使用C#与ADO.NET建立连接.请尽快给我答案.

谢谢与问候
Alok kumar

Hello please let me know how to make connection with ADO.NET using C#.please give me the answer as soon as possible.

Thanks and Regards
Alok kumar

推荐答案

您尝试过
http://connectionstrings.com/ [ ^ ]

这是有史以来最好的连接字符串资源.

您只需将连接字符串放在配置中,然后使用它打开一个新连接.
Did you try
http://connectionstrings.com/[^]

It is the best resource of connectionstrings ever.

You just put your connectionstring in your config and open a new connection using it.


除了第一个答案之外,您还可以签出 ^ ].
In addition to the first answer, you can check out http://www.aspnet101.com/2008/01/a-beginners-guide-to-the-connection-string/[^].


将此类放在您项目中的.cs文件中.添加一个新项(服务数据库),它将创建一个.mdf文件(MSSQL的数据库文件).双击文件,然后打开服务器资源管理器(视图菜单).打开属性选项卡,并获取ConnectionString属性.用连接字符串替换DataHelperMS类中的connectString字段.请加倍反斜杠,以避免编译器错误.

在事件处理程序中,只需创建一个新的DataHelperMS类并调用其ExecuteDataSet/ExecuteNonQuery方法.它应该完美工作
put this class on a .cs file in your project. Add a new item (Service Database) which will create a .mdf file (the database file for MSSQL). Double click on the file and open server explorer (view menu). Open the properties tab, and get the ConnectionString property. Replace the connectString field in my DataHelperMS class with the connection string. Please double the backslashes to avoid a compiler error.

In your event handler, just create a new DataHelperMS class and call its ExecuteDataSet / ExecuteNonQuery Methods. It should work perfectly
public class DataHelperMS
    {
        public static String connectString = "Data Source=.;AttachDbFilename=\"C:\\Users\\RR\\Documents\\Visual Studio 2008\\Projects\\PhilNits Exam Simulator\\PhilNits Exam Simulator\\exam.mdf\";Integrated Security=True;User Instance=True";

        public String ConnectionString
        {
            get {
         //       String connection = ConfigurationManager.ConnectionStrings["DataConnectionString"].ConnectionString;
           //     String result = String.Format(connection , Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("\\bin\\")+1 ) );
        //      result = connectString;
                return connectString;
             }
        }
        SqlConnection conn;
        public DataHelperMS()
        {
            String conString = ConnectionString;
            this.conn = new SqlConnection(conString);
        }
        
        public int ExecuteNonQuery(String sql)
        {
            int result = 0;
            try
            {
                conn.Open();
                SqlCommand command = conn.CreateCommand();
                command.CommandText = sql;
                result = command.ExecuteNonQuery();
            }
            catch (SqlException)
            {
                result = 0;
                throw;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public DataSet ExecuteDataSet(String sql)
        {
            // Ensure that the current culture is US-English 
            //Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            DataSet result = null;
            try
            {
                conn.Open();
                SqlCommand command = conn.CreateCommand();
                command.CommandText = sql;
                result = ConvertDataReaderToDataSet(command.ExecuteReader());
            }
            catch (SqlException ex)
            {
                ex.ToString();
                result = null;
                throw;
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
        public static DataSet ConvertDataReaderToDataSet(SqlDataReader reader)
        {
            DataSet dataSet = new DataSet();
            do
            {
                // Create new data table
                DataTable schemaTable = reader.GetSchemaTable();
                DataTable dataTable = new DataTable();
                if (schemaTable != null)
                {
                    // A query returning records was executed
                    for (int i = 0; i < schemaTable.Rows.Count; i++)
                    {
                        DataRow dataRow = schemaTable.Rows[i];
                        // Create a column name that is unique in the data table
                        string columnName = (string)dataRow["ColumnName"]; //+ "<C" + i + "/>";
                        // Add the column definition to the data table
                        DataColumn column = new DataColumn(columnName, (Type)dataRow["DataType"]);
                        try
                        {
                            dataTable.Columns.Add(column);
                        }
                        catch (DuplicateNameException)
                        {
                            int count = 0;
                            while (dataTable.Columns[columnName] != null)
                            {
                                columnName += count;
                                count++;
                            }
                            column.ColumnName = columnName;
                            dataTable.Columns.Add(column);
                        }
                    }
                    dataSet.Tables.Add(dataTable);
                    // Fill the data table we just created
                    while (reader.Read())
                    {
                        DataRow dataRow = dataTable.NewRow();
                        for (int i = 0; i < reader.FieldCount; i++)
                            dataRow[i] = reader.GetValue(i);
                        dataTable.Rows.Add(dataRow);
                    }
                }
                else
                {
                    // No records were returned
                    DataColumn column = new DataColumn("RowsAffected");
                    dataTable.Columns.Add(column);
                    dataSet.Tables.Add(dataTable);
                    DataRow dataRow = dataTable.NewRow();
                    dataRow[0] = reader.RecordsAffected;
                    dataTable.Rows.Add(dataRow);
                }
            }
            while (reader.NextResult());
            foreach (DataTable dt in dataSet.Tables)
            {
                dt.AcceptChanges();
            }
            return dataSet;
        }
        

    }


这篇关于连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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