无法使用SQL连接连接到数据库 [英] Can't connect to database using a SQL Connection

查看:99
本文介绍了无法使用SQL连接连接到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我要建立一个用于电影系统的数据库.我已经用SQL制作了数据库,现在我要做的是使用ado.net在控制台应用程序中显示数据库中的信息.现在,我已将代码放入此工作中,并且可以成功构建,但是运行时不会显示任何数据.现在,我认为这与数据源名称有关,我在SQL中使用的服务器名称为MASTER \ MASTER,但是当我为数据源名称输入此名称时,它会出现一条错误消息,内容为"Unregonized转义序列".因此要确认这一点,我将两个反斜杠而不是一个反斜杠放在一起,使它看起来像这样的MASTER \\ MASTER.这样可以解决该错误,但是该程序仍然无法正常工作.

这是我使用的代码,因此您可以查看它是否是此问题或其他可能导致我的程序无法正常工作的问题.

Hi, so i''m making a database which is for a film system. I''ve made the database in SQL, now what I want to do is to display information from the database in a console application using ado.net. Now i''ve put the code in for this work and it builds successfully but when it runs no data is displayed. Now I think this has something to do with the data source name, the server name i''ve used in SQL is MASTER\MASTER, but when I entered this in for the data source name it came up with an error message saying "Unregonized Escape Sequence". So to recify this I put in two backslashs instead of one so it looked like this MASTER\\MASTER. This resolved the error, but the program still doesnt work.

Heres the code i''ve used for it so you can see if it''s this problem or something else that might be causing my program not to work.

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // set up data connection
                string cs = "Data Source=MASTER\MASTER;Initial Catalog=FilmDB;Integrated Security=True;Pooling=False";
                SqlConnection cn = new SqlConnection(cs);
                // Set up SQL command
                SqlCommand cm = cn.CreateCommand();
                ///
                ///Sql Select Command (Read table and return data)
                ///
                cm.CommandText = "SELECT FilmID, FilmName FROM [dbo].[Film];";
                // Set up adapter manager
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cm;
                //Set dataset for results
                DataSet ds = new DataSet();
                ds.Clear();
                //Open Connection to database
                cn.Open();
                //Ask adapter to transfer data from table to dataset
                da.Fill(ds, "Film");
                //Set up data table
                DataTable dt = ds.Tables["Film"];
                //Read data from table rows
                foreach (DataRow dr in dt.Rows)
                {
                    Console.WriteLine(dr["FilmID"].ToString() + "\t" + dr["FilmName"].ToString());
                }
                //Close connection to database
                cn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            finally
            {
                //cn.close();
            }
        }
    }

推荐答案

在我看来,您的连接字符串不正确.一个简单的原因是,我认为您对数据源感到困惑,并且我认为您不会拥有服务器名称Master.

看看 http://www.connectionstrings.com/.这应该可以帮助您建立正确的连接字符串.
To me it looks like your connection string is incorrect. The simple reason is I think you are confused with the data source and I don''t think that you would be having a server name Master.

Have a look at http://www.connectionstrings.com/. This should help you building correct connection string.


首先,要么需要一起使用两个"\"字符,要么在字符串前面需要一个"@":
Firstly, either you will need two ''\'' characters together or an ''@'' in front of the string:
string cs = "Data Source=MASTER\\MASTER;Initial Catalog=FilmDB;Integrated Security=True;Pooling=False";





string cs = @"Data Source=MASTER\MASTER;Initial Catalog=FilmDB;Integrated Security=True;Pooling=False";


在字符串中,"\"引入了转义序列-您通常无法输入的字符,例如双引号或换行符-双反斜杠是反斜杠的转义序列. ``@''将转义反斜杠转义.

其次,检查您的连接字符串:如果在服务器资源管理器中单击数据库,则相应的连接字符串将显示在属性窗格中.

当您说程序仍然无法正常工作"时,您是什么意思?你有例外吗?如果是这样,该怎么办?如果没有,那么您会不会发生什么,或者您会不会发生什么?


In a string, ''\'' introduces an escape sequence - a character you can''t enter normally such as double quote, or new line - Double backslash is the escape sequence for backslash. The ''@'' turns backslash escaping off.

Secondly, check your connection string: if you click on a database in the Server Explorer, the appropriate connection string is shown in the properties pane.

When you say "the program still doesnt work" what do you mean? Do you get an exception? If so what? If not, what does happen that you aren''t expecting, or does happen that you aren''t?


这篇关于无法使用SQL连接连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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