C#Windows窗体:不支持SqlConnection关键字,连接超时

问题描述 投票:1回答:1

刚刚学习如何通过Tom Owsiak C#Windows窗体视频教程创建Windows窗体应用程序,我陷入了需要将数据存储到数据库的数据库项目(联系人管理系统)。

我一直在跟踪他的每一步,但不知何故设法弄乱应用程序编写过程。错误发生在该行

SqlConnection conn = new SqlConnection(connString);

一直在搜索stackExchange一段时间并尝试可能的解决方案,但仍然无法解决它。

// error occurs here, stated key word not supported, connection timeout
using (SqlConnection connectforfucksake = new SqlConnection(connString)) 
{
    try
    {
        connectforfucksake.Open(); // open the connection

        // create the new SqlCommand object
        command = new SqlCommand(insert, connectforfucksake); 

        command.Parameters.AddWithValue(@"Data_Added", dateTimePicker1.Value.Date);
        command.Parameters.AddWithValue(@"Company", txtCompany.Text);
        command.Parameters.AddWithValue(@"Website", txtWebsite.Text);
        command.Parameters.AddWithValue(@"Title", txtTitle.Text);
        command.Parameters.AddWithValue(@"First_Name", txtFName.Text);
        command.Parameters.AddWithValue(@"Last_Name", txtLName.Text);
        command.Parameters.AddWithValue(@"Address", txtAddress.Text);
        command.Parameters.AddWithValue(@"City", txtCity.Text);
        command.Parameters.AddWithValue(@"State", txtState.Text);
        command.Parameters.AddWithValue(@"Postal_Code", txtPostalCode.Text);
        command.Parameters.AddWithValue(@"Mobile", txtMobile.Text);
        command.Parameters.AddWithValue(@"Note", txtNote.Text);

        command.ExecuteNonQuery();   // pushing whatever in the form into table
    }
    catch (Exception ex)
    {
       MessageBox.Show(ex.Message); // show the unforeseen error
    }
}

期望应用程序获取结果然后将它们存储到数据库中,但似乎SqlConnection对象实例化导致错误。

c# database-connection connection-string sqlconnection
1个回答
1
投票

听起来你的连接字符串是完全错误的;最有可能的,你的意思是“连接超时”而不是“连接超时”。包含连接超时的基本连接字符串可能类似于:

Data Source=.;Initial Catalog=master;Integrated Security=True;Connect Timeout=42
© www.soinside.com 2019 - 2024. All rights reserved.