using System; using System.Data; using System.Data.SqlClient; using System.Collections.Generic; using System.Windows.Forms;namespace Test { static class ProDatabase { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmlogin());
} public static void ConnectDB() { SqlConnection conn = new SqlConnection("Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=."); conn.Open(); } } }是不是这样写哦
SqlConnection 要放在ConnectDB外边定义,否则外边访问不到
可是在winform开发中好象没有app.config中哦//
public static SqlConnection CreateConnection() { return new SqlConnection("server=.;database=Database;uid=sa;pwd=;"); }
再在需要調用的頁面這樣寫 SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strConnection"].Trim());
還有一種寫在 class裡面也行
“System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings” 我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
“System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings” 我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
“System.Configuration.ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings” 我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢 ============= 这只是个旧的用法而已, 只会有个Warning, 但不会有错的
{}
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Windows.Forms;namespace Test
{
static class ProDatabase
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmlogin());
}
public static void ConnectDB()
{
SqlConnection conn = new SqlConnection("Persist Security Info=False;User ID=sa;Initial Catalog=Test;Data Source=.");
conn.Open();
}
}
}是不是这样写哦
{
return new SqlConnection("server=.;database=Database;uid=sa;pwd=;");
}
在add.config的XML裡加上
<appSettings>
<add key="strConnection" value="" />
</appSettings>
value後面是你要連的數據庫
再在需要調用的頁面這樣寫
SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["strConnection"].Trim());
我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
我的例子是
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="strConnection" value="workstation id=WEIXIONG;packet size=4096;integrated security=SSPI;data source=WEIXIONG;persist security info=False;initial catalog=GoodWay" />
</appSettings>
</configuration>
我按照上面兄弟说的写可是报一上面的错误~~~~~请再指教!!谢谢
=============
这只是个旧的用法而已, 只会有个Warning, 但不会有错的
急急~~一個C#中select語句更新數據表的問題~~大蟲幫幫 這一張
<configuration>
<appSettings>
<add key="strConnection" value="workstation id=WEIXIONG;packet size=4096;integrated security=SSPI;data source=WEIXIONG;persist security info=False;initial catalog=GoodWay" />
</appSettings>
</configuration>==================== 先在Reference里面添加System.Configuration, 就会有System.Configuration.ConfigurationManager的引用了 System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
nvc = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
string connectionString = nvc["strConnection"];