且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数据库SQL Server与Visual Studio 2010连接起来?

更新时间:2022-05-24 23:04:03

通过下面的链接
http://www.dofactory.com/Connect/Connect.aspx [ http://connectionstrings.com/ [ ^ ]
谢谢
--RA
go through the below links
http://www.dofactory.com/Connect/Connect.aspx[^]

for connection strings for all databases
http://connectionstrings.com/[^]
Thanks
--RA


app.config文件,其连接字符串是这样的

app.config file with connection string like this

<configuration>
    <configsections>
    </configsections>
    <connectionstrings>
        <add name="ConnectionString">
            connectionString="Data Source=MyPC\MyDB;Initial Catalog=MyDB;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </add></connectionstrings>
</configuration>





连接到db





Connecting to db

class DBConnection
    {
        #region Database connection method
        public SqlConnection Connect()
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
 
            try
            {
                conn.Open();
            }
            catch
            {
            }
            return conn;
        }
        #endregion
    }



谢谢

SP



Thanks

SP


用于在Visual Studio中进行连接
打开服务器资源管理器
右键单击添加连接
遵循简单的步骤
创建连接后,右键单击数据库
点击属性
在属性"中将找到连接字符串.
复制此字符串.


For connectivity in visual studio
open Server Explorer
right click add Connection
Follow simple steps
After Creating Connection Right click on Database
Click on properties
In Proprties Connection String will be found.
Copy This String.


private void button1_Click(object sender, EventArgs e)
       {
           string cmd="select *from Admin where ID='"+txtFilenO.Text+"' and Password='"+textBox1.Text+"' ";
          SqlConnection con=new SqlConnection();
 
           con.connectionString="Paste Connection String";
            con.Open();
SqlCommand cmd1 = new SqlCommand(cmd,con);
 
           SqlDataReader dr = cmd1.ExecuteReader();
           if (dr.Read())
           {
               AdminMenu n = new AdminMenu();
               n.lblsser.Text = dr[0].ToString();
               n.Show();
               this.Dispose();
               this.Close();
               cmd1.Connection.Close();
 
           }
           else
           {
               cmd1.Connection.Close();
               MessageBox.Show("ID/Password is not valid");
           }
 
       }


这是来自向导的连接,但是您应该了解每个关键字的含义,因此请参见
连接字符串

如果要将Connectiion字符串放入app.config中


it is connection from wizard,But You should learn meaning of each keyword, So see
Connection Strings

if you want to put Connectiion string in app.config

<appsettings>
    <add key="cnn" value="your connection string" />
  </appsettings>


并用作


and use as

string str = System.Configuration.ConfigurationSettings.AppSettings["cnn"].ToString();
           OleDbConnection con = new OleDbConnection(str);
           con.Open();