繁体   English   中英

这是将数据库(.mdf文件)连接到Visual Studio项目的正确方法吗?

[英]Is this the correct way for connecting database (.mdf file) to visual studio project?

为此,我没有得到任何输出,认为没有任何错误或警告...我猜问题出在连接字符串上,但不确定,是吗? 我已经在SQL Server数据工具中创建了数据库名称RV(.mdf文件),并将其连接到Visual Studio中的该项目。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;

namespace student_TableConnectTry1
{



    class Program
    {


        static void Main(string[] args)
        {
            using (SqlConnection cn = new SqlConnection())
            {


                    cn.ConnectionString =@"Data Source=(local);Integrated Security=SSPI;" +"Initial Catalog=RV";

                cn.Open();



                string strSQL = "Select * From student";
                SqlCommand myCommand = new SqlCommand(strSQL, cn);
                using (SqlDataReader myDataReader = myCommand.ExecuteReader())
                {
                    // Loop over the results.
                    while (myDataReader.Read())
                    {

                        Console.WriteLine("-> usn: {0}, name: {1}.",
                        myDataReader["usn"].ToString(),
                        myDataReader["name"].ToString()
                        );
                    }
                }

                Console.ReadLine();

            }
        }
    }
}

您可以使用以下连接字符串并尝试:

Integrated Security=SSPI;Persist Security Info=False;User ID=youruserid;Initial Catalog=databasename;Data Source=.\SQLEXPRESS

另请参见以下链接以获取连接字符串:

http://www.connectionstrings.com/sql-server/

希望对您有帮助。 :)

让我知道这是否对您没有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM