silverlight连接SQL Server数据库

2017-01-07 19:01:33来源:CSDN作者:qq_32493189人点击

第七城市

1.和sqlite基本相同,只是连接语句略有差别

using System.Data;using System.Data.SqlClient;using System.Web.Configuration;/// <summary>        /// 查找所有数据        /// </summary>        /// <param></param>        /// <returns></returns>        static public DataSet DBselect(string sql)        {            string conn;            conn = WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;            try            {                using (SqlConnection cnn = new SqlConnection(conn))                {                    cnn.Open();                    using (SqlTransaction trans = cnn.BeginTransaction(IsolationLevel.Serializable))                    {                        try                        {                            using (SqlCommand cmd = new SqlCommand(sql, cnn))                            {                                cmd.Transaction = trans;                                using (DataSet ds = new DataSet())                                {                                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))                                    {                                        da.Fill(ds);                                        trans.Commit();                                    }                                    return ds;                                }                            }                        }                        catch (Exception ex)                        {                            trans.Rollback();                            return new DataSet();                            throw ex;                        }                        finally                        {                        }                    }                }            }            catch (Exception ex)            {                return new DataSet();                throw ex;            }        }

2.在web.config中加入数据源信息

<appSettings>    <add key="DBappSetting" value="Data Source=(localdb)/Projects;Initial Catalog=SLRiaTest;Integrated Security=True;Connect Timeout=100;Encrypt=False;TrustServerCertificate=False"/>  </appSettings>  <connectionStrings>    <add name="myConnectionString" connectionString="Data Source=(LocalDB)/v11.0;AttachDbFilename=C:/Users/User/Documents/database.mdf;Integrated Security=True;Connect Timeout=30"/>  </connectionStrings>

3.在SQL server 中新建数据库信息,查看属性Data Source 替换connectionString即可

第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台