Wednesday, August 25, 2010

关于C#小数位数

1.简单的例子

System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();

provider.NumberDecimalDigits =intDecLength; //要設定的小數位數

double strCashAmt=Convert.ToDouble(this.txtCashAmt.Text); //先把控件內的值轉成double



this.txtCashAmt.Text = strCashAmt.ToString("N",provider); //再利用ToString函數格式化小數位數

2.C#保留小数位N位,四舍五入 .

decimal d= decimal.Round(decimal.Parse("0.55555"),2);

3.C#保留小数位N位四舍五入

Math.Round(0.55555,2)

4,C#保留小数位N位四舍五入

double dbdata = 0.55555;

string str1 = dbdata.ToString("f2");//fN 保留N位,四舍五入

5.C#保留小数位N位四舍五入

string result = String.Format("{0:N2}", 0.55555);//2位



string result = String.Format("{0:N3}", 0.55555);//3位

6. C#保留小数位N位四舍五入

double s=0.55555;

result=s.ToString("#0.00");//点后面几个0就保留几位


C#保留小数位数,及百分号的解决方法:

1、用NumberFormatInfo类来解决:

System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();



provider.PercentDecimalDigits = 2;//小数点保留几位数.

provider.PercentPositivePattern = 2;//百分号出现在何处.

double result = (double)1 / 3;//一定要用double类型.

Response.Write(result.ToString("P", provider));

2、用toString方法.:

public string getRate(double hcount, double task)

{

string rValue;

string temp = "";



if (task == 0)

{

task = 1;

}



double db = (hcount / task) * 100;



if (hcount >= task)

{

rValue = "100%";

}

else

{

rValue = db.ToString("#0.#0") + "%";

}

return rValue;

}



string str1 = String.Format("{0:N1}",56789); //result: 56,789.0

string str2 = String.Format("{0:N2}",56789); //result: 56,789.00

string str3 = String.Format("{0:N3}",56789); //result: 56,789.000

string str8 = String.Format("{0:F1}",56789); //result: 56789.0

string str9 = String.Format("{0:F2}",56789); //result: 56789.00

string str11 =(56789 / 100.0).ToString("#.##"); //result: 567.89

string str12 =(56789 / 100).ToString("#.##"); //result: 567

Monday, August 16, 2010

Install solr in windows tomcat

1. download solr like apache-solr-1.4.1
2. download tomcat 5.5 or above
3. Install tomcat
4. Extract apache-solr-1.4.1 package to anywhere
5. Copy apache-solr-1.4.1\dist\apache-solr-1.4.1.war to Tomcat webapps folder, for instance: Tomcat 5.5\webapps
6. Start tomcat and get: Tomcat 5.5\webapps\apache-solr-1.4.1 directory
7. Stop tomcat, open Tomcat 5.5\webapps\solr\WEB-INF\web.xml and copy below

<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>C:\solr</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>

to replace the old.
8. Zip apache-solr-1.4.1 and rename to solr.zip
9. Delete Tomcat 5.5\webapps\apache-solr-1.4.1 directory
10. Copy apache-solr-1.4.1\example\solr folder to c:
11. restart tomcat

That's it.

Monday, August 2, 2010

SQL Server Express Connection String

1. Working with SQL Client:
1.1 NameSpace: System.Data.SqlClient;
1.2 Objects: DataSet dataSet;
SqlConnection conn;
SqlDataAdapter dbAdapter;
1.3 Connection Strings:

1.3.1 Using Windows Authentication Mode of Connectivity:
string conString = "Server=.\\SQLEXPRESS;" +
"DataBase=yourDatabase;" +
"Integrated Security=SSPI;";

1.3.2 Using SQL Server Authentication Mode of Connectivity:
string conString = "Server=.\\SQLEXPRESS;" +
"DataBase=yourDatabase;" +
"Uid=yourUserId;Pwd=yourPassword;";

2. Working with OLEDB:
2.1 NameSpace: System.Data.OleDb;
2.2 Objects: DataSet dataSet;
OleDbConnection conn;
OleDbDataAdapter dbAdapter;
2.3 Connection Strings:

2.3.1 Using Windows Authentication Mode of Connectivity:
string conString = "Provider=SQLOLEDB;" +
"Data Source=.\\SQLEXPRESS;" +
"Initial Catalog=yourDataBase;" +
"Integrated Security=SSPI;";

2.3.2 Using SQL Server Authentication Mode of Connectivity:
string conString = "Provider=SQLOLEDB;" +
"Data Source=.\\SQLEXPRESS;" +
"Initial Catalog=yourDataBase;" +
"Uid = yourUserId; Pwd = yourPassword;";

xml operation

<p>用的是一种很笨的方法,但可以帮助初学者了解访问XML节点的过程。

已知有一个XML文件(bookstore.xml)如下:

</p><table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td> <p><?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" isbn="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>
</p> </td> </tr> </tbody> </table> <p>

1、往<bookstore>节点中插入一个<book>节点:

</p> <p> </p><table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td> <p> XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性

XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);

root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");
</p> </td> </tr> </tbody> </table> <p>
//================
结果为:

</p> <p> </p><table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td> <p><?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" isbn="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" isbn="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>
</p> </td> </tr> </tbody> </table> <p>
2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。

</p><table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td> <p> XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”

XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
}

xmlDoc.Save("bookstore.xml");//保存。
</p> </td> </tr> </tbody> </table>

//=================

最后结果为:

<table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td> <p><?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" isbn="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" isbn="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>
</p> </td> </tr> </tbody> </table>

3、删除 <book genre="fantasy" isbn="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" isbn="2-3631-4">节点。

<table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td> <p>XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;

foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;</p> </td> </tr> </tbody> </table>
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");

//====================

最后结果为:

<table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td><?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book isbn="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore></td> </tr> </tbody> </table>

4、显示所有数据。

<table rules="none" align="center" bgcolor="#ddedfb" border="1" bordercolor="#55aaff" cellpadding="0" cellspacing="0" width="500"> <tbody> <tr> <td width="10">
</td> <td> <p> XmlNode xn=xmlDoc.SelectSingleNode("bookstore");

XmlNodeList xnl=xn.ChildNodes;

foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"));

XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
} </p> </td> </tr> </tbody> </table> <p> </p> <p>
转载自:http://blog.csdn.net/s98/archive/2004/10/17/140307.aspx


XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("c:/aaa.xml");
XmlNode xn=xmlDoc.SelectSingleNode("todate");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;

XmlNodeList xnf1=xe.ChildNodes;

Response.Write(xnf1[0].InnerText);//显示子节点点文本
} </p> <p> XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("c:/aaa.xml");
XmlNode xn=xmlDoc.SelectSingleNode("todate");
XmlNodeList xnl=xn.ChildNodes;
foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;

XmlNodeList xnf1=xe.ChildNodes;

xnf1[0].InnerText="10";
xmlDoc.Save("c:/aaa.xml");</p>