【发布时间】: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