using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace ProjectLion.Components
{
/// <summary>
/// Summary description for ProjectDB.
/// </summary>
public class ProjectDB
{
//取得記錄集(連接SQL數據庫)
public static SqlDataReader GetRdBySQL(string strSQL)
{
SqlDataReader MyReader;
SqlConnection myConnection = new SqlConnection( ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection);
myCommand.CommandType = CommandType.Text; try
{
myConnection.Open();
MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
return null;
} // Return the datareader MyReader
return MyReader;
}
//執行一條SQL語句
public static Boolean ExecuteStrSQL(string strSQL)
{
//連接SQL數據庫
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection);
myCommand.CommandType = CommandType.Text; // Execute the command
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch
{
return false;
}
return true;
} //取得字段值
public static string GetFdBySQL(string strSQL, string strField)
{
string Result=""; //連接SQL數據庫
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection); myCommand.CommandType = CommandType.Text;
myConnection.Open();
SqlDataReader MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // Return the datareader result
while (MyReader.Read())
{
Result = MyReader[strField].ToString ();
}
return Result;
} //判斷是否空記錄
public static Boolean CheckFdBySQL(string strSQL)
{
//連接SQL數據庫
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection); myCommand.CommandType = CommandType.Text;
myConnection.Open();
SqlDataReader MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // Return the datareader result
while (MyReader.Read())
{
return true;
}
return false;
} //取得字段ID
public static int GetTableID(string strColumn)
{
string strSQL;
int TableID =1;
//連接SQL數據庫
strSQL = "SELECT TableID FROM BaseDataID WHERE Code = '" + strColumn + "'";
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection); myCommand.CommandType = CommandType.Text;
myConnection.Open();
SqlDataReader MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // Return the TableID
while (MyReader.Read())
{
TableID = Int32.Parse(MyReader["TableID"].ToString()) + 1;
strSQL = " UPDATE BaseDataID SET TableID = " + TableID.ToString() +
" WHERE Code = '" + strColumn.ToString() + "'";
if (ExecuteStrSQL(strSQL))
{
return TableID;
}
else
{
return 0;
}
} strSQL = " INSERT INTO BaseDataID(Code,Name,TableID) VALUES('" +
strColumn.ToString() + "','" +
strColumn.ToString() + "'," +
TableID.ToString() + ")"; if (ExecuteStrSQL(strSQL))
{
return TableID;
}
else
{
return 0;
}
} }
}
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace ProjectLion.Components
{
/// <summary>
/// Summary description for ProjectDB.
/// </summary>
public class ProjectDB
{
//取得記錄集(連接SQL數據庫)
public static SqlDataReader GetRdBySQL(string strSQL)
{
SqlDataReader MyReader;
SqlConnection myConnection = new SqlConnection( ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection);
myCommand.CommandType = CommandType.Text; try
{
myConnection.Open();
MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch
{
return null;
} // Return the datareader MyReader
return MyReader;
}
//執行一條SQL語句
public static Boolean ExecuteStrSQL(string strSQL)
{
//連接SQL數據庫
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection);
myCommand.CommandType = CommandType.Text; // Execute the command
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch
{
return false;
}
return true;
} //取得字段值
public static string GetFdBySQL(string strSQL, string strField)
{
string Result=""; //連接SQL數據庫
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection); myCommand.CommandType = CommandType.Text;
myConnection.Open();
SqlDataReader MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // Return the datareader result
while (MyReader.Read())
{
Result = MyReader[strField].ToString ();
}
return Result;
} //判斷是否空記錄
public static Boolean CheckFdBySQL(string strSQL)
{
//連接SQL數據庫
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection); myCommand.CommandType = CommandType.Text;
myConnection.Open();
SqlDataReader MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // Return the datareader result
while (MyReader.Read())
{
return true;
}
return false;
} //取得字段ID
public static int GetTableID(string strColumn)
{
string strSQL;
int TableID =1;
//連接SQL數據庫
strSQL = "SELECT TableID FROM BaseDataID WHERE Code = '" + strColumn + "'";
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["StrSQLConnection"]);
SqlCommand myCommand = new SqlCommand(strSQL, myConnection); myCommand.CommandType = CommandType.Text;
myConnection.Open();
SqlDataReader MyReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); // Return the TableID
while (MyReader.Read())
{
TableID = Int32.Parse(MyReader["TableID"].ToString()) + 1;
strSQL = " UPDATE BaseDataID SET TableID = " + TableID.ToString() +
" WHERE Code = '" + strColumn.ToString() + "'";
if (ExecuteStrSQL(strSQL))
{
return TableID;
}
else
{
return 0;
}
} strSQL = " INSERT INTO BaseDataID(Code,Name,TableID) VALUES('" +
strColumn.ToString() + "','" +
strColumn.ToString() + "'," +
TableID.ToString() + ")"; if (ExecuteStrSQL(strSQL))
{
return TableID;
}
else
{
return 0;
}
} }
}
===========================
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconsampleapplication.asp要操作数据库,需要对ADO.NET有所了解。您可以可访问下缅这个网址以了解更多的信息:ADO.NET Datasets
http://msdn.microsoft.com/library/en-us/vbcon/html/vboriDatasets.asp-微软全球技术中心 VC技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。