如何在C#应用程序中定义和使用连接字符串 [英] How do I define and use a connection string in my C# app

查看:85
本文介绍了如何在C#应用程序中定义和使用连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想知道如何定义连接字符串并在我的应用程序中成功使用它。想法是定义一次并根据需要使用。

我按照这里的建议:



如何从C#中的App.Config获取连接字符串 [ ^ ]



收效甚微。当我运行我的测试应用程序时,它运行正常,但当我要求它将唯一值插入我为该场合创建的测试数据库时,我有一条错误消息:



对象引用未定义到对象的实例在语句的级别

Hi I wish to know how to define a connection string and use it successfully in my App. The Idea is to define once and use as required.
I followed the advice here :

How to get Connection String from App.Config in C#[^]

with little success. when i run my test app, it runs ok but when i ask it to insert sole values into the test database I created for the occasion i have an error message:

" Object reference is not defined to an instance of the object " at the level of the statement

var connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;





我应该做些什么来解决这个问题呢?



我尝试过:



1.首先我在config.app文件中添加了这段代码:





What should I do to get this situation sorted out please ?

What I have tried:

1. First I added this code in the config.app file:

<connectionStrings>
    <add name= "con" 
         providerName= "System.Data.SqlClient" 
         connectionString="Data Source=.\sqlexpress;Initial Catalog=practice;Integrated Security=True" />
    </connectionStrings>





2.接下来我添加了对System.Configuration的引用



3.我的按钮控件的click事件包含以下代码:





2. Next I Added a reference to System.Configuration

3. The click event of my button control has the code :

var connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            
            string ins = @"insert into employee(name, address, phonenum, profession) values(@nam, @add, @phonen, @job)";

                SqlCommand cmd = new SqlCommand(ins, con);
                SqlParameter NM = new SqlParameter();
                NM.ParameterName = "nam";
                NM.Value = nametxt.Text;

                SqlParameter AD = new SqlParameter();
                AD.ParameterName = "add";
                AD.Value = addresstxt.Text;

                SqlParameter PN = new SqlParameter();
                PN.ParameterName= "phonen";
                PN.Value = telephonetxt.Text;

                SqlParameter PF = new SqlParameter();
                PF.ParameterName = "job";
                PF.Value = professiontxt.Text;

                cmd.Parameters.Add(AD);
                cmd.Parameters.Add(PN);
                cmd.Parameters.Add(NM);
                cmd.Parameters.Add(PF);
                con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Values Entered", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                con.Close();

推荐答案

嘿伙计们,我找到了缺少的小东西:

这实际上是这样的:



Hey Guys, I found the little thing that was lacking:
This actually did it:

private void btn_enter_Click(object sender, EventArgs e)
{
var connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
 
SqlConnection cn= new SqlConnection(connectionString); //This is all I had to add
string ins = @"insert into employee(name) values(@nam)";

SqlCommand cmd = new SqlCommand(ins, cn);//Brought in a few modifications 
SqlParameter NM = new SqlParameter();
NM.ParameterName = "nam";
NM.Value = txtboxname.Text;
cmd.Parameters.Add(NM);
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Values Entered", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
cn.Close();


这篇关于如何在C#应用程序中定义和使用连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆