如何将数据库SQL Server与Visual Studio 2010连接起来? [英] how to connect database SQL Server with Visual Studio 2010?

查看:112
本文介绍了如何将数据库SQL Server与Visual Studio 2010连接起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我是使用C#编程的新程序员.我不知道如何将Microsoft SQL Server 2008数据库与Visual Studio 2010连接起来.我将应用程序创建为窗口应用程序.请帮我?我想知道连接数据库完成后是否有一个文件app.config,那么我们必须创建一个文件来连接数据库吗?

谢谢所有,

Dear all,

I am a new programmer with C# programming. I don''t know how to connect database Microsoft SQL server 2008 with Visual Studio 2010. I create an application as a window application. Please help me? I wonder that when connect with database finish so we have a file app.config, then we have to create a file to connect database more?

Thanks all,

推荐答案

通过下面的链接
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();


这篇关于如何将数据库SQL Server与Visual Studio 2010连接起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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