本文介绍了从app.config读取的connectionstring的类的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在app.config中创建一个ConnectionString,我试图通过我创建的类初始化它。



App.config:



 < ?xml version =   1.0 encoding =   utf-8>  
<配置>
< startup>
< supportedRuntime version = v4.0 sku = 。NETFramework,Version = v4.5 />
< / startup >
< connectionStrings>
< add name = EQCas
connectionString = 数据源= solutionworx-pc\sqlexpress;初始目录= eqcas;集成安全性=真; />
< / connectionStrings >
< / configuration >





申请:



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Configuration;
使用 System.Data.SqlClient;

命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
new SQLConnect();
SqlDataReader myReader = null ;
SqlCommand myCommand = new SqlCommand( select x_id,fullname,email FROM cas_user_ext WHERE fullname = @fullname);
myCommand.Parameters.AddWithValue( @ username Ivan Lubbe);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader [ x_id]);
Console.WriteLine(myReader [ fullname]);
Console.WriteLine(myReader [ email]);
}
}
}
}

public SQLConnect
{
public string myConnection;

public SQLConnect()
{
myConnection = ConfigurationManager.ConnectionStrings [ EQCas]。ConnectionString;
SqlConnection connectSQL = new SqlConnection(myConnection);
connectSQL.Open();
}
}





我假设我使用的是我创建错误的类。大多数教程都不清楚如何使用

类,因为我试图使用它。我是C#的新手,现在已经使用了大约4个星期。非常感谢任何帮助。此外,一旦我的连接字符串工作,我将尝试创建一个类来从SQL获取/设置值不同于上面的代码(仅用于测试目的)



这个是我已经完成了我得到的建议。



v2



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Configuration;
使用 System.Data.SqlClient;

命名空间 ConsoleApplication1
{
class 计划
{
静态 void Main( string [] args)
{
SQLConnect sqlcon = new SQLConnect();
SqlDataReader myReader = null ;
SqlCommand myCommand = new SqlCommand( select x_id,fullname,email FROM cas_user_ext WHERE fullname = @fullname
sqlcon.Connection);
myCommand.Parameters.AddWithValue( @ fullname Ivan Lubbe);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader [ x_id]);
Console.WriteLine(myReader [ fullname]);
Console.WriteLine(myReader [ email]);
}
sqlcon.Close();
}
}
}

public class SQLConnect
{
public string myConnection;
public SqlConnection Connection { get ; private set ; }

public SQLConnect()
{
myConnection = ConfigurationManager.ConnectionStrings [ EQCas]。ConnectionString;
// SqlConnection connectSQL = new SqlConnection(myConnection);
// connectSQL.Open();
Connection = new SqlConnection(myConnection);
Connection.Open();
}

内部 void 关闭()
{
Connection.Close();
}
}





还有什么我应该改进的吗?或者我应该阅读的具体内容是什么?

解决方案


I have tried to create a ConnectionString within my app.config and i'm trying to initialize it via a class i have created.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="EQCas"
         connectionString="Data Source=solutionworx-pc\sqlexpress;Initial Catalog=eqcas;Integrated Security=True;"/>
  </connectionStrings>
</configuration>



Application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            new SQLConnect();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select x_id, fullname, email FROM cas_user_ext WHERE fullname = @fullname");
            myCommand.Parameters.AddWithValue("@username", "Ivan Lubbe");
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                Console.WriteLine(myReader["x_id"]);
                Console.WriteLine(myReader["fullname"]);
                Console.WriteLine(myReader["email"]);
            }
        }
    }
}

public class SQLConnect
{
    public string myConnection;

    public SQLConnect()
    {
        myConnection = ConfigurationManager.ConnectionStrings["EQCas"].ConnectionString;
        SqlConnection connectSQL = new SqlConnection(myConnection);
        connectSQL.Open();
    }
}



I assume i am using the class i created wrong. Most tutorials are not very clear on how to use
classes as i have tried to use it. I am new to C# been using it for around 4 weeks now. Would appreciate any assistance. Also once i have got my connection string working i will attempt to create a class to get/set values from SQL unlike the code above (just for testing purposes)

This is what i have done with the advice i have gotten.

v2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SQLConnect sqlcon = new SQLConnect();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("select x_id, fullname, email FROM cas_user_ext WHERE fullname = @fullname",
                                                                                    sqlcon.Connection);
            myCommand.Parameters.AddWithValue("@fullname", "Ivan Lubbe");
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                Console.WriteLine(myReader["x_id"]);
                Console.WriteLine(myReader["fullname"]);
                Console.WriteLine(myReader["email"]);
            }
        sqlcon.Close();
        }
    }
}

public class SQLConnect
{
    public string myConnection;
    public SqlConnection Connection { get; private set; }

    public SQLConnect()
    {
        myConnection = ConfigurationManager.ConnectionStrings["EQCas"].ConnectionString;
        //SqlConnection connectSQL = new SqlConnection(myConnection);
        //connectSQL.Open();
        Connection = new SqlConnection(myConnection);
        Connection.Open();
    }

    internal void Close()
    {
        Connection.Close();
    }
}



Anything else that i should improve on? Or specific things i should read up on?

解决方案



这篇关于从app.config读取的connectionstring的类的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:35