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

C# Windows Forms: SqlConnection keyword not supported, connection timeout

刚刚学习如何通过 Tom Owsiak C# 创建 Windows Forms 应用程序 Windows Forms 视频教程,我被困在需要将数据存储到的数据库项目(联系人管理系统)中一个数据库。

我一直在关注他的每一步,但不知何故设法搞砸了应用程序编写过程。错误发生在

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 对象实例化导致了错误。

听起来您的连接字符串完全错误;很可能,您的意思是 "Connect Timeout" 而不是 "connection timeout"。包含连接超时的基本连接字符串可能类似于:

Data Source=.;Initial Catalog=master;Integrated Security=True;Connect Timeout=42