Wednesday, January 27, 2010

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
SqlConnection connecotion = new SqlConnection("Data Source=.;Initial Catalog=liaotian;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Timer1_Tick(object sender, EventArgs e)
{
connecotion.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("select * from yonghu where zaixian='1'", connecotion);
DataSet set = new DataSet();
Adapter.Fill(set);
ListBox1.DataSource = set;
ListBox1.DataTextField = "name";//5~1-a-s-p-x
ListBox1.DataValueField = "name";
ListBox1.DataBind();

}
protected void Button1_Click(object sender, EventArgs e)
{
Application.Set("jilu",Application["jilu"] + "
" + Convert.ToString(Session["name"]) + "说:" + TextBox2.Text);
TextBox2.Text = "";
}

}

Parameters.AddWithValue method in SQL statement Where the usage of the words

It was questioned, Parameters.AddWithValue method in some cases so bad that he's written like this:

string strWhere = "'%good%'";
strSql = "SELECT * FROM area Where [name] like @strWhere";//it is no use
cmd.Parameters.AddWithValue("@strWhere", strWhere);

This is because, ASP.NET generated SQL statements and will be followed by the Like a single quotation mark, causing the error, if you open the SQL Server track manager, you can see the implementation of the statement is as follows:

exec sp_executesql N'SELECT * FROM Article Where [Title] like @strWhere',N'@strWhere nvarchar(5)',@strWhere=N'%why%'

Not difficult to understand, in the OldDbCommand will be similar in approach. The correct code is:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>

ACCESS to the "time / date" field when you insert a DateTime.Now "Standard expression data type mismatch." Wrong solution

In the Use the following code to add data to the Access database, when, and if the date field, there will be "standard does not match the data type of expression." Error, which may be the date of the type in C # can not be directly converted into Access the date in the type of OleDbType.DBDate result of:

string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataDirectory\MengXianHui.mdb;Persist Security Info=True";
string QueryString = "Insert Into [Document] (Title, Content, Author, CreateDate) Values(@Title, @Content, @Author, @CreateDate)";
OleDbConnection cn = new OleDbConnection(ConnectionString);
cn.Open();
OleDbCommand cmd = new OleDbCommand(QueryString, cn);
cmd.Parameters.AddWithValue("@Title", Title);
cmd.Parameters.AddWithValue("@Content", Content);
cmd.Parameters.AddWithValue("@Author", Author);
cmd.Parameters.AddWithValue("@CreateDate", DateTime.Now);
cmd.ExecuteNonQuery();
cn.Close();
cn.Dispose();


Solution is the above statement cmd.Parameters.AddWithValue ( "@ CreateDate", DateTime.Now); can be converted to the following statement:

OleDbParameter parameter = new OleDbParameter();
parameter.OleDbType = OleDbType.DBDate;
parameter.Value = DateTime.Now;
cmd.Parameters.Add(parameter);

view.aspx.cs

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;

public partial class view : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (DropDownList1.SelectedValue)
{
case "1":
Image1.ImageUrl = "png.aspx?aa=1";
break;
case "2":
Image1.ImageUrl = "png.aspx?aa=2";
break;
case "3":
Image1.ImageUrl = "png.aspx?aa=3";
break;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox1.Text == Session["gif"].ToString())
Response.Write("OK,正确");
else
Response.Write("验证码不符合");
}
}

generate multi-column table through the cycle of reading DataReader

int toShowColumn = 4;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string cnString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataDirectory\aspxWeb.mdb;";
OleDbConnection cn = new OleDbConnection(cnString);
cn.Open();
OleDbCommand cmd = new OleDbCommand("Select * from Document", cn);
OleDbDataReader dr = cmd.ExecuteReader();
sb.Append("");
while (dr.Read())
{
sb.Append("");
sb.Append("");
for (int i = 0; i < toShowColumn - 1; i++)
{
if (dr.Read())
{
sb.Append("");
}
else
{
sb.Append("");
}
}
sb.Append("");
}
sb.Append("
");
sb.Append(dr["Title"].ToString());
sb.Append("
");
sb.Append(dr["Title"].ToString());
sb.Append("
");
div1.InnerHtml = sb.ToString();
dr.Close();
cn.Close();
cn.Dispose();