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



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

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

SqlConnection conn = new SqlConnection(connString);

已经搜索堆栈交换一段时间并尝试可能的解决方案,但仍然无法解决。

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

听起来您的连接字符串完全错误;最有可能的是,您的意思是"连接超时"而不是"连接超时"。包含连接超时的基本连接字符串可能如下所示:

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

相关内容

最新更新