简体   繁体   English

在Winform C#中定义Sql Connection时出错

[英]Error at define Sql Connection in winform c#

I write this code in my app.config: 我在我的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>

My c# code is : 我的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();

        }
    }
}

But there is an error on this line: 但是这条线上有一个错误:

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["miztahrirtest2DB"].ToString());

and said : 并表示 :

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 错误1类型或名称空间名称'ConfigurationManager'在名称空间'System.Configuration'中不存在(您是否缺少程序集引用?)D:\\ visual studio project \\ Project \\ LocalService1 \\ LocalService1 \\ signup.cs 35 72 LocalService1

You should add a reference System.Configuration.dll to your project. 您应该将引用System.Configuration.dll添加到您的项目中。

Right click on project, from Add menu, click Reference... and from the dialog, search for System.Configuration.dll and click the checkbox to check it, then click ok. 右键单击项目,从“ Add菜单中,单击“ Reference...然后从对话框中搜索System.Configuration.dll ,然后单击复选框进行检查,然后单击“确定”。

  • If you are using VS2010 you can't search and you should select the dll from .Net tab 如果您使用的是VS2010,则无法搜索,而应从.Net选项卡中选择dll。
  • If you are using VS2013 you can select Assemblies -> Framework node. 如果使用的是VS2013,则可以选择“程序集->框架”节点。
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["localservice"].ToString());

在web.config中,您被赋予了不同的名称,但是在C#中, ConnectionStrings [“ localservice”]中被赋予了不同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM