Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C连接到sql server 2008数据库#_C#_Sql Server - Fatal编程技术网

C# 使用C连接到sql server 2008数据库#

C# 使用C连接到sql server 2008数据库#,c#,sql-server,C#,Sql Server,我尝试使用连接字符串进行连接,但出现以下错误: “初始化字符串的格式不符合从索引0开始的规范。” 这是我的密码: SqlConnection Con = new SqlConnection(@"C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10.SQLEXPRESS\MSSQL\DATA\TARGIL3.MDF"); SqlDataAdapter adapt = new SqlDataAdapter(); adapt.

我尝试使用连接字符串进行连接,但出现以下错误: “初始化字符串的格式不符合从索引0开始的规范。”

这是我的密码:

   SqlConnection Con = new SqlConnection(@"C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10.SQLEXPRESS\MSSQL\DATA\TARGIL3.MDF");
        SqlDataAdapter adapt = new SqlDataAdapter();
        adapt.InsertCommand = new SqlCommand(" INSERT INTO tblEmployee VALUES (@employeeNumber, @employeePrivateName, @employeeFamilyName ,@city, @street, @houseNo, @phoneNumber, @birthDate, @startWorkingDate)", Con);
        adapt.InsertCommand.Parameters.Add("@employeeNumber", SqlDbType.Char).Value = textBox1.Text;
        adapt.InsertCommand.Parameters.Add("@employeePrivateName", SqlDbType.VarChar).Value = textBox2.Text;
        adapt.InsertCommand.Parameters.Add("@employeeFamilyName", SqlDbType.VarChar).Value = textBox3.Text;
        adapt.InsertCommand.Parameters.Add("@city", SqlDbType.VarChar).Value = textBox4.Text;
        adapt.InsertCommand.Parameters.Add("@street", SqlDbType.VarChar).Value = textBox5.Text;
        adapt.InsertCommand.Parameters.Add("@houseNo", SqlDbType.Int).Value = textBox6.Text;
        adapt.InsertCommand.Parameters.Add("@phoneNumber", SqlDbType.Char).Value = textBox7.Text;
        adapt.InsertCommand.Parameters.Add("@birthDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);
        adapt.InsertCommand.Parameters.Add("@startWorkingDate", SqlDbType.DateTime).Value = Convert.ToDateTime(textBox8.Text);


        Con.Open();
        adapt.InsertCommand.ExecuteNonQuery();
        Con.Close();

您的连接字符串是错误的。如果使用SQL Server身份验证,则应采用以下形式:

Server=myServerAddress;Database=myDataBase;User Id=myUsername;
Password=myPassword;
其中
myServerAddress
是运行SQL Server的计算机的IP或主机名。如果是你的电脑,
localhost
可能会工作

myUsername
myPassword
是连接到服务器的用户的凭据

如果使用受信任连接,则连接字符串应为以下形式:

Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

您可以在此处查看更多示例:

您不能只在连接字符串中输入文件名

C:\PROGRAM FILES\MICROSOFT SQL SERVER\MSSQL10.SQLEXPRESS\MSSQL\DATA\TARGIL3.MDF
Sql Server通常是一个“服务”,您可以与服务对话

现在。如果您使用的是“Express”版本……则可能必须包含AttachDbFilename参数

但是如果您不使用“Express”,那么您应该只输入有关Sql Server服务的信息(machine\instancename等),而不关心.MDF的物理路径

Sql Server不是JET(访问)。对于JET,您只需指定文件名

如果您仍然有问题,您应该发布您正在使用的sql server版本…..执行“选择@@version”并报告该问题。

使用以下方法:

string connString = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME; User ID = USERNAME; Password=PASSWORD;Integrated Security=false";
SqlConnection Con = new SqlConnection(connString);

您不会像那样连接到sql server数据库。选中此链接,您应该连接到数据库实例名称,而不是它后面的某个物理文件。看起来您的代码也缺少第一行的后半部分!如何通过名称进行连接?还要检查此链接将连接字符串更改为类似以下内容的内容
“数据源=您的服务器;初始目录=您的数据库;用户ID=您的SQLUSEID;密码=您的密码”
string connString = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME; User ID = USERNAME; Password=PASSWORD;Integrated Security=false";
SqlConnection Con = new SqlConnection(connString);