[問題] 連SQL SERVER沒問題,但無法切換資料庫?

作者: Competitive (...)   2014-05-27 08:51:32
各位好
目前在看一支程式(程式碼在最後)
目前使用正常,但問題在於SqlConnection
("server=(local)\\SQLEXPRESS;database=abc;uid=sa;password=abc"
在SQL SERVER中還有另一個資料庫為abc2
但我的SQLEXPRESS中的database如果改為abc2,仍會連abc
請問在C#中切換資料庫還要作什麼處理嗎?
還是函式中CALL的DATABASECONNECT()其實是另一隻程式的
問題很菜很淺,但仍請指教
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace AAA
{
public partial class AddEmp : System.Web.UI.Page
{
protected DataSet myDataSet;
SqlConnection conn;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["EmpID"] == null)
{
Response.Write("<script>window.parent.location.href='Logon.aspx';</script>");
}
else
{
if (!IsPostBack)
{
Button1.Attributes["onclick"] = "javascript:return
confirm('確定加入?');";
InitDropItems();
bind();
}
}
}
private void DatabaseConnect()
{
conn = new
SqlConnection("server=(local)\\SQLEXPRESS;database=abc;uid=sa;password=abc");
conn.Open();
}
protected void InitDropItems()
{
try
{
DatabaseConnect();
//煦?蹈桶
SqlCommand tmpcmd = new SqlCommand("Select * from DropItems
where ItemName='煦?' and Visible=1", conn);
tmpcmd.CommandType = CommandType.Text;
SqlDataAdapter tmpadp = new SqlDataAdapter();
tmpadp.SelectCommand = tmpcmd;
DataSet tmpds = new DataSet("ddlTeam");
tmpadp.Fill(tmpds);
作者: Competitive (...)   2014-05-27 08:55:00
是否像JSP一樣其實要先編譯成CLASS才能運作?
作者: johnpage (johnpage)   2014-05-27 13:03:00
連線字串怪怪的
作者: netnk (不平凡的常人...)   2014-05-28 22:48:00
conn.close()

Links booklink

Contact Us: admin [ a t ] ucptt.com