在 winform c# 中定义 Sql 连接时出错
Error at define Sql Connection in winform c#
我在 app.config:
中写了这段代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="localservice" providerName="System.Data.SqlClient"
connectionString="Data Source=GROOT\SQL;Initial Catalog=localservice;Integrated Security=True" />
</connectionStrings>
</configuration>
我的 C# 代码是:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
private void button1_Click(object sender, EventArgs e)
{
int radius = 10;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());
SqlCommand cmd = new SqlCommand("insert into vendor (username,password,lenght,width,radius,category) values (@username,@password,@lenght,@width,@radius,@category);SELECT SCOPE_IDENTITY()", con);
cmd.Parameters.AddWithValue("username", textBox1.Text);
cmd.Parameters.AddWithValue("password", textBox2.Text);
cmd.Parameters.AddWithValue("lenght", Convert.ToInt32(textBox3.Text));
cmd.Parameters.AddWithValue("width", Convert.ToInt32(textBox4.Text));
cmd.Parameters.AddWithValue("radius", radius);
cmd.Parameters.AddWithValue("category", comboBox1.Text);
con.Open();
cmd.ExecuteNonQuery();
//Int32 classid = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
}
但是这一行有错误:
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());
并说:
Error 1 The type or namespace name 'ConfigurationManager' does not
exist in the namespace 'System.Configuration' (are you missing an
assembly reference?) D:\visual studio
project\Project\LocalService1\LocalService1\signup.cs 35 72 LocalService1
您应该向您的项目添加引用 System.Configuration.dll
。
右键单击项目,从 Add
菜单中单击 Reference...
,然后从对话框中搜索 System.Configuration.dll
并单击复选框以选中它,然后单击确定。
- 如果您使用的是 VS2010,则无法搜索,您应该 select 来自 .Net 选项卡的 dll
- 如果您使用的是 VS2013,则可以 select 程序集 -> 框架节点。
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localservice"].ToString());
您在 web.config 中获得了不同的名称,但在 C# 中,您在 ConnectionStrings["localservice"]
中获得了不同的名称
我在 app.config:
中写了这段代码<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="localservice" providerName="System.Data.SqlClient"
connectionString="Data Source=GROOT\SQL;Initial Catalog=localservice;Integrated Security=True" />
</connectionStrings>
</configuration>
我的 C# 代码是:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
private void button1_Click(object sender, EventArgs e)
{
int radius = 10;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());
SqlCommand cmd = new SqlCommand("insert into vendor (username,password,lenght,width,radius,category) values (@username,@password,@lenght,@width,@radius,@category);SELECT SCOPE_IDENTITY()", con);
cmd.Parameters.AddWithValue("username", textBox1.Text);
cmd.Parameters.AddWithValue("password", textBox2.Text);
cmd.Parameters.AddWithValue("lenght", Convert.ToInt32(textBox3.Text));
cmd.Parameters.AddWithValue("width", Convert.ToInt32(textBox4.Text));
cmd.Parameters.AddWithValue("radius", radius);
cmd.Parameters.AddWithValue("category", comboBox1.Text);
con.Open();
cmd.ExecuteNonQuery();
//Int32 classid = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
}
但是这一行有错误:
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());
并说:
Error 1 The type or namespace name 'ConfigurationManager' does not exist in the namespace 'System.Configuration' (are you missing an assembly reference?) D:\visual studio project\Project\LocalService1\LocalService1\signup.cs 35 72 LocalService1
您应该向您的项目添加引用 System.Configuration.dll
。
右键单击项目,从 Add
菜单中单击 Reference...
,然后从对话框中搜索 System.Configuration.dll
并单击复选框以选中它,然后单击确定。
- 如果您使用的是 VS2010,则无法搜索,您应该 select 来自 .Net 选项卡的 dll
- 如果您使用的是 VS2013,则可以 select 程序集 -> 框架节点。
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localservice"].ToString());
您在 web.config 中获得了不同的名称,但在 C# 中,您在 ConnectionStrings["localservice"]
中获得了不同的名称