C#学习心得

C# SQL数据库在外地连接解决方法:

Form1:
private void Form1_Load(object sender, System.EventArgs e)
{
Form2 F2=new Form2();
F2.ShowDialog();
if(F2.DialogResult==DialogResult.OK)
{
string cnStr="uid="+F2.stru1.user+";password="+F2.stru1.pw+";database="+F2.stru1.dbase+
";server=localhost";
SqlConnection cn=new SqlConnection(cnStr);
}
}

Form2:
1、先建立一个struct,并构造:
public struct stru
{
public string dbase,user,pw;
public stru(string Dbase,string User,string Pw)
{
dbase=Dbase;user=User;pw=Pw;
}
}
public stru stru1=new stru("","","");
2、在Form2上建立2个按钮,一个“确定”按钮,其dialogResult设为None;另一个“取消”按钮,其dialogResult设为“Cancel”;
private void button1_Click(object sender, System.EventArgs e)
{
stru1.dbase=tb1.Text;
stru1.user=tb2.Text;
stru1.pw=tb3.Text;
string cnStr="uid="+stru1.user+";password="+stru1.pw+";database="+stru1.dbase+";server=localhost";
cn.ConnectionString=cnStr;
try
{
cn.Open();
MessageBox.Show("OK,您的数据库连接成功!");
this.DialogResult=DialogResult.OK;
//在FORM1中,DialogResult只有在不是None的情况下,可以接收已经关闭的Form2的存储的结果
//另外,如果按钮没有设置,那么虽然按钮的DialogResult=None,但窗体却是Cancel.
this.Close();//这样就可以达到如果数据库连接失败,就不关闭此窗口,连接成功,才关闭此窗口。
}
catch (Exception ex)
{
MessageBox.Show("对不起,你的数据库连接失败!");
}
}
private void button2_Click(object sender, System.EventArgs e)
{
stru1.dbase="";
stru1.user="";
stru1.pw="";
}

 

DataList的翻页问题:

<%@ Page Language="C#" debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load( Object sender , EventArgs e)
{
//Build the Grid only if the page has been accessed for the first time
if( !IsPostBack )
BuildGrid();
}

public void BuildGrid()
{
SqlConnection myConnection =
new SqlConnection(
"server=(local)//NetSDK;database=Northwind;Trusted_Connection=yes" );
SqlDataAdapter myAdapter =
new SqlDataAdapter(
"SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products" ,
myConnection);
//Fill the DataSet
DataSet ds = new DataSet();
//Get the startRecord Count
//Remember database count is zero based so first decrease the value of
//the current page
int startRecord = ( int.Parse( CurrentPage.Value ) - 1 ) *
int.Parse( PageSize.Value );
//Fetch only the necessary records.
myAdapter.Fill( ds , startRecord , int.Parse( PageSize.Value ) ,
"Products");
//DataBind the Repeater
MyRepeater.DataSource = ds.Tables["Products"].DefaultView;
MyRepeater.DataBind();

//Second Part
//Create a new Command to select the total number of records
SqlCommand myCmd = new SqlCommand( "SELECT Count(*) from Products",
myConnection );
myConnection.Open();
//retrieve the value
TotalSize.Value = myCmd.ExecuteScalar().ToString() ;
myConnection.Close();
BuildPagers();
}

public void Page_Repeater( object sender, EventArgs e )
{
//Check for Button clicked
if( ((LinkButton)sender).ID == "Prev" )
{
//Check if we are on any page greater than 0
if( ( int.Parse( CurrentPage.Value ) - 1 ) >= 0 )
{
//Decrease the CurrentPage Value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) - 1 ).ToString() ;
}
}
else if( ((LinkButton)sender).ID == "Next" )
{
//Check if we can display the next page.
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
< int.Parse( TotalSize.Value ) )
{
//Increment the CurrentPage value
CurrentPage.Value = ( int.Parse( CurrentPage.Value ) + 1 ).ToString() ;
}
}
//Rebuild the Grid
BuildGrid();
}

public void BuildPagers()
{
//Check if its possible to have the previous page
if( ( int.Parse( CurrentPage.Value ) - 1 ) <= 0 )
{
Prev.Enabled = false;
}
else
{
Prev.Enabled = true ;
}
//Check if its possible to have the next page
if( ( int.Parse( CurrentPage.Value ) * int.Parse( PageSize.Value ) )
>= int.Parse( TotalSize.Value ) )
{
Next.Enabled = false;
}
else
{
Next.Enabled = true ;
}
}
</script>
<body>
<h1>Products Listing</h1>
<form runat="server">
<ASP:Repeater id="MyRepeater" runat="server">
<HeaderTemplate>
<table width="100%" border="1" cellpadding="1" cellspacing="2">
<tr>
<th>
Product ID
</th>
<th>
Product
</th>
<th>
Quantity Per Unit
</th>
<th>
Unit Price
</th>
</tr>

</HeaderTemplate>

<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "ProductID") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "ProductName") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "QuantityPerUnit") %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "UnitPrice", "$ {0}") %>
</td>
</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</ASP:Repeater>
<input type="hidden" id="PageSize" value="10" runat="server">
<input type="hidden" id="CurrentPage" value="1" runat="server">
<input type="hidden" id="TotalSize" runat="server">
<asp:LinkButton id="Prev" Text="<< Previous" OnClick="Page_Repeater" runat="server" />
&nbsp;
<asp:LinkButton id="Next" Text="Next >>" OnClick="Page_Repeater" runat="server" />
</form>
</body>
</html>

 

很强的c#.net函数列表


1、DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)

2、Int32.Parse(变量) Int32.Parse("常量")
字符型转换 转为32位数字型

3、 变量.ToString()
字符型转换 转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500.00%


4、变量.Length 数字型
取字串长度:
如: string str="中国";
int Len = str.Length ; //Len是自定义变量, str是求测的字串的变量名

5、System.Text.Encoding.Default.GetBytes(变量)
字码转换 转为比特码
如:byte[] bytStr = System.Text.Encoding.Default.GetBytes(str);
然后可得到比特长度:
len = bytStr.Length;

6、System.Text.StringBuilder("")
字符串相加,(+号是不是也一样?)
如:System.Text.StringBuilder sb = new System.Text.StringBuilder("");
sb.Append("中华");
sb.Append("人民");
sb.Append("共和国");

7、变量.Substring(参数1,参数2);
截取字串的一部分,参数1为左起始位数,参数2为截取几位。
如:string s1 = str.Substring(0,2);

8、String user_IP=Request.ServerVariables["REMOTE_ADDR"].ToString();
取远程用户IP地址

9、穿过代理服务器取远程用户真实IP

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值