- -太累了本想发昨天那个的第二篇~~结果~~只能把早就准备好的法了~~哈哈原谅我哈![10.gif](https://www.cnblogs.com/Emoticons/QQ/10.gif)
首先看个我自己画的结构图~~艺术品![dateset.gif](https://i-blog.csdnimg.cn/blog_migrate/ad43f62976d9f6eb49b564fdd0dadf4a.gif)
- -顺便说说这也是我比较系统地学习~~建议大家卖本书~~然后坐例子~~当然我这得例子~~有些是原创的德的德困死了.....
复习下
1---
DbConnection con=new SqlConnection();
con.ConnectionString="连接字符串"
2-连接Odbc--
Driver=用于连接的ODBC驱动
Dsn-以一个数据源名称
Server=所要连接服务器的名称
Trusted_Connection=根据当前登录的用户所使用的域名帐户指定安全措施
DataBase=所要连接的数据库
DBQ=物理路径
3-Server
Data Source=数据库服务器的名称或者是Ip,本机用 .
dataBase=数据库的名称
user
pwd
Enlist ,在设置为 ture的时候-,连接池(pooler)自动将连接列入调用者线程的当前事务上下文中
Pooling=true请求从连接池中取出一个新的连接,如果不存在连接池,则创建他
Max Pool Size-最大的连接池数量默认是100
Min Pool Size-最小连接数默认是 0
async=true时支持异步
Connection Reset=默认为true-从连接池中删除连接的时候会重置连接
MultipleActiveResultSers=在设置为true的时候,准许从同一个连接上获取多个只进只读的结果集,默认是false
//---得到网站的根目录的物理路径
Response.Write(AppDomain.CurrentDomain.BaseDirectory as string);
//-----------得到网站根目录+数据库文件夹的物理路径
Response.Write(AppDomain.CurrentDomain.GetData("DataDirectory") as string);
我们把SqlServer某个Db解剖了~~看看他老的构造
//-----------------------------------读取整个数据库和指定数据库的信息
页面<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ic.aspx.cs" Inherits="ic" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:GridView
ID
="GridView1"
runat
="server"
OnSelectedIndexChanged
="GridView1_SelectedIndexChanged"
OnSelectedIndexChanging
="GridView1_SelectedIndexChanging"
>
<
Columns
>
<
asp:CommandField
ShowSelectButton
="True"
/>
</
Columns
>
</
asp:GridView
>
</
div
>
<
asp:GridView
ID
="GridView2"
runat
="server"
>
</
asp:GridView
>
</
form
>
</
body
>
</
html
>
//--------------------代码
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;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
partial
class
ic : System.Web.UI.Page
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.DataGriveBin(this.GridView1, "");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
GridView1.EditIndex= e.NewSelectedIndex;
this.DataGriveBin(this.GridView1, "");
TextBox tm = GridView1.Rows[e.NewSelectedIndex].Cells[1].Controls[0] as TextBox;
this.DataGriveBin(this.GridView2, tm.Text);
}
//为传入的GridView邦定构架信息,-------根据传近来的命令选择,如果是空则邦定全局构架,不是空邦定具体的一个
public void DataGriveBin(GridView temp, string Command)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Data.SqlClient.SqlConnection my = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString());
DataTable sa = null;
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
my.Open();
if (Command == "")
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//--空读取全部
sa = my.GetSchema();
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//--不是空则读取指定的构造
sa = my.GetSchema(Command);
}
temp.DataSource = sa;
temp.DataBind();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
catch (System.Data.SqlClient.SqlException ex)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
finally
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
my.Close();
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
你联想到什么了么??对了就拿些自动生成实体类的咚咚惯用的![14.gif](https://www.cnblogs.com/Emoticons/QQ/14.gif)
Ok完大了还是看看基础把---DataColumn 总结
DataColumn vin=new DataColumn("vin");
vin.DataType =typeof(string);
vin.MaxLength=23,-1这意味这不会对最大长度检查
vin.Unique=true;--是否准许重复的
vin.AllowDBNull=默认为 True,可以没有数据
vin.Caption=标题
DataTable.PrimaryKey=new DataColumn[]{vin};//---设置标题
DataTable.Load(dataRow,LoadOption枚举);//利用datarow中的主建更新DataTable中有相同的主建的列,并把更新后的DataRow和更新前的DataRow设置成LoadOption枚举需要的样子
LoadOption枚举
OverwriteChanges将原来的行和新行共同设置为Unchanged
PreserveChanges不该原来行的设置,将新行设置为;Unchanged
Upsert:不该原来行的设置,新航的设置为Added,如果新行和旧行的设置一样的时候则它们的设置为
,Unchanged,如果它们不同,则设置为Modified
~~太麻烦了看个例子吧
DataRow newAuto
=
DataTable.NewRow();
newAuto[
"
vin
"
]
=
"
1234
"
;
newAuto[
"
Make
"
]
=
"
Ford
"
;
DataTalbe.Rows.add(newAuto);
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
DataTaLbe.LoadDataRow(
new
oject[]
{"1234","ttttt"}
,LoadOption.OverWriteChanges);
//
----------更新1234
//--------------------------------DataRow
Detached-DataRow创建了但是没有加到某个DataTable中
Added-创建了dataroe并添加到DataTable中
Unchanged--自从上一次调用AcceptChanges方法后还有没有修改DataRow对象,在调用AcceptChanges方法时 ,DataRow对象 时DataRow将改为这个状态
Modified--自上一次调用AcceptChanges方法后已经修改了DataRow对象
Deleted-使用DataRow类的Delete方法删除了DataRow对象
---------DataRow生存其变化:测试数据
DataRow MyRow = myDataTable.NewRow();--->RowState=Detached, 创建还没有添加到DataTable中
myDataTable.Rows.Add(MyRow);--->RowState=Added, 创建并且添加到DataTable中
MyRow["ddd"] = "aaa";--->RowState=Added, 创建并且添加到DataTable中
myDataTable.RejectChanges();--->RowState=Detached, 回滚自该表家在以来到上次调用提交方法,之间的数据,所以该Row状态为-创建还没有添加到DataTable中
myDataTable.Rows.Add(MyRow); myDataTable.AcceptChanges();--->RowState=Unchanged,
MyRow["ddd"] = "aaa";--->RowState=Modified, 自上次调用.AcceptChanges();后修改了数据
MyRow.Delete();--->RowState=Deleted, 使用DataRow删除DataRow对象
不清楚的兄弟姐妹用下面的代码测试下就知道了~~原创对了上面那个也是~~
测试
-
用的代码:
DataTable myDataTable
=
new
DataTable();
myDataTable.Columns.Add(
new
DataColumn(
"
ddd
"
,
typeof
(String)) );
DataRow MyRow
=
myDataTable.NewRow();
Response.Write(
string
.Format(
"
DataRow MyRow = myDataTable.NewRow();--->RowState={0}, 创建还没有添加到DataTable中
"
, MyRow.RowState.ToString()));
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myDataTable.Rows.Add(MyRow);
Response.Write(
string
.Format(
"
<br>myDataTable.Rows.Add(MyRow);--->RowState={0}, 创建并且添加到DataTable中
"
, MyRow.RowState.ToString()));
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
MyRow[
"
ddd
"
]
=
"
aaa
"
;
Response.Write(
string
.Format(
"
<br>MyRow[\
"
ddd\
"
] = \
"
aaa\
"
;--->RowState={0}, 创建并且添加到DataTable中
"
, MyRow.RowState.ToString()));
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
myDataTable.RejectChanges();
Response.Write(
string
.Format(
"
<br>myDataTable.RejectChanges();--->RowState={0}, 回滚自该表家在以来到上次调用提交方法,之间的数据,所以该Row状态为-创建还没有添加到DataTable中
"
, MyRow.RowState.ToString()));
myDataTable.Rows.Add(MyRow);
myDataTable.AcceptChanges();
Response.Write(
string
.Format(
"
<br> myDataTable.Rows.Add(MyRow); myDataTable.AcceptChanges();--->RowState={0},
"
, MyRow.RowState.ToString()));
MyRow[
"
ddd
"
]
=
"
aaa
"
;
Response.Write(
string
.Format(
"
<br>MyRow[\
"
ddd\
"
] = \
"
aaa\
"
;--->RowState={0}, 自上次调用.AcceptChanges();后修改了数据
"
, MyRow.RowState.ToString()));
MyRow.Delete();
Response.Write(
string
.Format(
"
<br>MyRow[\
"
ddd\
"
] = \
"
aaa\
"
;--->RowState={0}, 使用DataRow删除DataRow对象
"
, MyRow.RowState.ToString()));
~~哈哈听说DataRow-在运行时有多个版本~~不信么???
Current--在数据发生变化后DataRow当前的数据--只有DataRowState的数值为Deleted否则该版本的数据都存在
Default-如果DataRowState的值为Added或Modified,则默认版本是current.如果DataRowState的数值为Deleted,则抛出异常 ,如果执行了
BeginEdit方法,则为Propsed
Original--其数值为原先DataRow中加载的数据,或者是上一次执行AcceptChanges方法的数值,注意只有在DataRowState的数值为Modified,Unchanged或
Deleted是该版本才会存在,如果DataRowState的数值为Deleted,则该信息可获取的,如果DataRowState为Added则抛出异常
Proposed-在编辑DataRow对象的数值.如果DataRowState的数值为Deleted,则报告异常,如果还未显示的执行BeginEdit方法,或如果通过编辑一个独立的 DataRow对象一个还没有添加到DataTable中的对象中孤立DataRow对象,来隐式地执行BeginEdit方法则报错
DataRow-对象包含一个HasVersion-方法-可以查找DataRowVersion=的数值是否存在
//--实在睁不开眼睛了~~最后的例子~~原创~~阿阿阿![09.gif](https://www.cnblogs.com/Emoticons/QQ/09.gif)
protected
void
Page_Load(
object
sender, EventArgs e)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataTable myDataTable = new DataTable();
myDataTable.Columns.Add(new DataColumn("ddd",typeof(String)) );
myDataTable.Columns.Add(new DataColumn("dd1", typeof(String)));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataRow MyRow = myDataTable.NewRow();
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MyRow["ddd"] = "aaa";
MyRow["dd1"] = "222";
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//---------使用RejectChanges()使用心得
//myDataTable.RejectChanges();//回滚时,在上次调用AcceptChanges()或初始化之间的数据都会消失比如上面的 myDataTable.Rows.Add(MyRow);
//添加这行有与属于没有调用提交方法所以行被删除,且 MyRow["ddd"] = "aaa",给行添加的数据也不存在
//一旦行调用了提交方法AcceptChanges();行的回滚方法RejectChanges();只能回滚提交方法后修改得数据或状态
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
myDataTable.Rows.Add(MyRow);
MyRow.AcceptChanges();
myDataTable.AcceptChanges();
myDataTable.RejectChanges();//虽然调用回滚方法单上面已经调用了提交方法股数据不受到影响
Response.Write(string.Format("<br> myDataTable.Rows.Add(MyRow); myDataTable.AcceptChanges();--->RowState={0}, ", MyRow.RowState.ToString()));
MyRow["ddd"] = "aaa";
Response.Write(string.Format("<br>MyRow[\"ddd\"] = \"aaa\";--->RowState={0}, 自上次调用.AcceptChanges();后修改了数据", MyRow.RowState.ToString()));
MyRow.Delete();
Response.Write(string.Format("<br>MyRow[\"ddd\"] = \"aaa\";--->RowState={0}, 使用DataRow删除DataRow对象", MyRow.RowState.ToString()));
this.GridView1.DataSource = this.GetDataRowInfo(MyRow);
GridView1.DataBind();
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// 在DataRowVersion中检测dataRow中的某行的某个状态中数据是否存在
/// </summary>
/// <param name="row">要检测的行</param>
/// <param name="columnName">要检测的列</param>
/// <returns>返回检测状态</returns>
private
string
GetDataRowInfo(DataRow row,
string
columnName)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
System.Text.StringBuilder retVal = new System.Text.StringBuilder(string.Format("RowState:{0}*", row.RowState));
foreach(string vers in Enum.GetNames(typeof(DataRowVersion)))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//retVal.Append(string.Format("Version:{0};value:{1}*", vers, row[columnName].ToString()));
DataRowVersion version = (DataRowVersion)Enum.Parse(typeof(DataRowVersion), vers);
if (row.HasVersion(version))
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
retVal.Append(string.Format("Version:{0};value:{1}*", version, row[columnName,version].ToString()));
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
retVal.Append(string.Format("Version:{0};value:{1}*", version, "does not exist"));
}
}
return retVal.ToString();
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// 在分析一个行中所有列的数据版本
/// 返回数值不一样,参数个数一样不够成重载
/// </summary>
private
DataTable GetDataRowInfo(DataRow row)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
System.Text.StringBuilder retVal = new System.Text.StringBuilder(string.Format("RowState:{0}\r\n", row.RowState));
DataTable retTable = new DataTable();
retTable.Columns.Add(new DataColumn("RowState", typeof(string)));
retTable.Columns.Add(new DataColumn("ColumnName", typeof(string)));
retTable.Columns.Add(new DataColumn(DataRowVersion.Current.ToString(), typeof(string)));
retTable.Columns.Add(new DataColumn(DataRowVersion.Default.ToString(), typeof(string)));
retTable.Columns.Add(new DataColumn(DataRowVersion.Original.ToString(), typeof(string)));
retTable.Columns.Add(new DataColumn(DataRowVersion.Proposed.ToString(), typeof(string)));
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (DataColumn tempColumn in row.Table.Columns)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//调用重载得到一个列的数据版本情况
string[] version=new string[this.GetDataRowInfo(row, tempColumn.ColumnName).Split('*').Length];
version = this.GetDataRowInfo(row, tempColumn.ColumnName).Split('*');
DataRow temp = retTable.NewRow();
temp[0] = version[0].Split(':')[1];
temp[1] = tempColumn.ColumnName;
temp[2] = version[1].ToString().Split(';')[1].Split(':')[1];
temp[3] = version[2].Split(';')[1].Split(':')[1];
temp[4] = version[3].Split(';')[1].Split(':')[1];
temp[5] = version[4].Split(';')[1].Split(':')[1];
retTable.Rows.Add(temp);
}
return retTable;
}