简体   繁体   English

使用SQL Server的应用程序中的SqlException

[英]SqlException in app using SQL Server

Hey I just started learning to make application in C# using back-end as sql. 嘿,我刚刚开始学习使用后端作为sql在C#中制作应用程序。 I put some breakpoints to see that the control never comes back after executing the cntCeilInn.Open(); 我放置了一些断点,以查看执行cntCeilInn.Open();之后该控件永不返回cntCeilInn.Open(); soon, the output window shows ' System.Data.SqlClient.SqlException ' occurred in System.Data.dll 不久,在输出窗口显示“ System.Data.SqlClient.SqlException ”发生在System.Data.dll

Google tells me its not timing out or something that I couldn't understand. Google告诉我它没有超时或我无法理解的事情。 Some help please. 请一些帮助。

string strServerName="EXPRESSION";

using (SqlConnection cntCeilInn = new SqlConnection("Data Source=" + strServerName + ";" + "Integrated Security=YES"))
{
    SqlCommand cmdCeilInn= new SqlCommand("If exists ("+ "Select name "+ "from sys.database "+ "Where name=N'CeilInn1')"+ "DROP database CeilInn1;"+ "go"+ "create database CeilInn1;", cntCeilInn);
    cntCeilInn.Open();
    cmdCeilInn.ExecuteNonQuery();
}

As connectionString parameter,you should use: 作为connectionString参数,您应该使用:

Data Source=yourServerName;Initial Catalog=yourDatabaseName;Integrated Security=True

make sure to modify youServerName and yourDatabaseName by appropriate data. 确保通过适当的数据修改youServerNameyourDatabaseName

Note that you forgot to set the database name in your connectionString( Initial Catalog ), and the value for Integrated Security is True . 请注意,您忘记在connectionString( Initial Catalog )中设置数据库名称,而Integrated Security的值为True

There are few mistakes in your sql query . sql查询中几乎没有错误。

  1. It's sys.databases not sys.database 它是sys.databases而不是sys.database

  2. No space between Go and Create syntax GoCreate语法之间没有空格

  3. You need to place the sql statements in seperate line 您需要将sql语句放在单独的行中

  4. Not to mention do check your connection string once as others have stated 更不用说像其他人所说的那样检查您的connection string

Try this out 试试看

SqlCommand cmdCeilInn = new SqlCommand("If exists (" + "Select name " + "from sys.databases " + "Where name=N'Sample') DROP database Sample;" 
                      +Environment .NewLine+ "go " + Environment .NewLine 
                     +" create database Sample;", cntCeilInn);

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

相关问题 将数据存储到SQL Server中的SqlException - SqlException on storing data into SQL Server 将linq2sql应用程序从SQL Server 2005升级到2008时出现SqlException - SqlException when upgrading linq2sql app from SQL Server 2005 to 2008 将OLEDB转换为SQL Server之后的.updateall中的SqlException - SqlException in .updateall after convert OLEDB to SQL Server 启动与SQL Server的连接时出现异常的SqlException - Unusual SqlException when initiating connection to SQL Server 尝试连接到SQL Server数据库时出现“ SqlException未处理”错误 - 'SqlException was unhandled' error when trying to connect to SQL Server database 数据库未启动但 SQL Server 已准备好连接时的 SqlException - SqlException when the database is not started but SQL Server is ready for connections 如果Sql Server返回错误,则应抛出SqlException。 但这不是 - If Sql Server returns an error, then SqlException should be thrown. But it isn't 使用Take in SQL LINQ查询:语法差异导致挂起和SQLException? - Using Take in SQL LINQ query: syntactic differences cause a hang and SQLException? Windows App和SQL Server - Windows App and SQL Server 尝试在SQL Server 2008 R2上调用某个列时,在C#上未处理SqlException - SqlException unhandled on C# when trying to call a certain column on SQL Server 2008 R2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM