【问题标题】:Throwing Exception When Saving To Database保存到数据库时抛出异常
【发布时间】:2014-03-27 03:20:30
【问题描述】:

我在将文本保存到 SQL 数据库时遇到问题,即 save() 方法。 这是我收到的错误消息。

“System.Data.SqlClient.SqlException”类型的未处理异常 发生在 System.Data.dll

附加信息:网络相关或特定于实例的错误 在建立与 SQL Server 的连接时发生。服务器是 未找到或无法访问。验证实例名称是否为 正确,并且 SQL Server 配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位 服务器/实例指定)

这是我的代码,我想知道问题出在哪里。有关如何解决此问题的任何建议?我将不胜感激!

public Form1()
{
    InitializeComponent();
}

private void accountDetailsBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    this.Validate();
    this.accountDetailsBindingSource.EndEdit();
    this.tableAdapterManager.UpdateAll(this.database1DataSet);
}

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'database1DataSet.AccountDetails' table. You can move, or remove it, as needed.
    this.accountDetailsTableAdapter.Fill(this.database1DataSet.AccountDetails);
}

private void nextButton_Click(object sender, EventArgs e)
{
    Amount = decimal.Parse(amount_InvestedTextBox.Text);
    WeekInterestRate = decimal.Parse(WeekIntTextBox.Text);
    TwoWeekInterestRate = decimal.Parse(TWeekIntTextBox.Text);
    MonthInterestRate = decimal.Parse(MonthIntTextBox.Text);
    ThreeMonthInterestRate = decimal.Parse(ThreeMonthIntTextBox.Text);
    Save();
    Change();
}

private void Save()
{
    SqlConnection cn = new SqlConnection(global::WindowsFormsApplication13.Properties.Settings.Default.Database1ConnectionString);
    string sql = "INSERT INTO AccountDetails (Amount Invested) values('" + amount_InvestedTextBox + ")";
    SqlCommand exesql = new SqlCommand(sql, cn);
    cn.Open();
    exesql.ExecuteNonQuery();
    this.accountDetailsTableAdapter.Fill(this.database1DataSet.AccountDetails);
    cn.Close();
}

private void Change()
{
    Form2 f2 = new Form2();
    f2.Show(this);
    Hide();
}

配置文件

 <configuration>
<configSections>
</configSections>
<connectionStrings>
    <add name="WindowsFormsApplication13.Properties.Settings.Database1ConnectionString"
        connectionString="Data Source=|DataDirectory|\Database1.sdf"
        providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>

【问题讨论】:

  • 您的连接字符串有问题,您可以在配置文件中找到。
  • 您可以在此连接上读取或保存任何其他数据吗?只是这个字段吗?
  • 发布 global::WindowsFormsApplication13.Properties.Settings.Default.Database1ConnectionString 的值
  • @jmcilhinney 谢谢,它说配置没有声明,用配置文件更新了主要问题
  • 您没有连接到您的数据库。检查您的 sql server 服务是否正在运行。如果没有,则启动它,然后尝试。

标签: c# sql sql-server database


【解决方案1】:

你为什么要创建一个SqlConnection?您有一个类型化的 DataSet,因此与数据库的所有交互都应该通过表适配器进行。如果要将记录插入到表中,请使用与该表对应的表适配器。

问题的直接原因是您正在使用仅用于 SQL Server 的 SqlConnection,其连接字符串用于 SQL Server CE。尽管名称不同,但 SQL Server 和 SQL Server CE 是完全不同的,并且每个都有一个单独的 ADO.NET 提供程序。不过,您完全不必担心这一点,因为正如我所说,您应该使用表适配器,它们包含所有连接、数据适配器和命令对象。

【讨论】:

    【解决方案2】:

    您的连接字符串有问题。检查连接字符串中的服务器名称和实例名称。 Check here了解更多详情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 2021-07-21
      • 2013-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多