|
|
|
ช่วยดูโค้ดให้หน่อยครับตรง C# StringBuilder sb.Clear(); มันเออเรอ อยากทราบว่าจะแก้ยังไง |
|
|
|
|
|
|
|
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using System.Data;
public partial class Post : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strConn;
strConn = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection Conn = new SqlConnection(strConn);
Conn.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT TOP 1 TID FROM Topic ORDER BY TID Desc");
var sqlSave = sb.ToString();
string LastTID = "";
SqlCommand com = new SqlCommand();
SqlDataReader dr;
com.Connection = Conn;
com.CommandType = CommandType.Text;
com.CommandText = sqlSave;
dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
var CurrentTID = dr.GetString(0);
int Running = Convert.ToInt32(CurrentTID.Substring(1, CurrentTID.Length - 1));
Running++;
LastTID = "T" + Running.ToString("0000000");
}
else
{
LastTID = "T0000001";
}
dr.Close();
sb.Clear();
sb.Append("INSERT INTO Topic(TID,Topic,Description,UserName,RecordDate,ViewCount,ReplyCount,IP)");
sb.Append("VALUES (@TID,@Topic,@Description,@UserName,@RecordDate,@ViewCount,@ReplyCount,@IP)");
sqlSave = sb.ToString();
com.CommandText = sqlSave;
com.Parameters.Clear();
com.Parameters.Add("@TID",SqlDbType.NVarChar).Value = LastTID;
com.Parameters.Add("@Topic",SqlDbType.NVarChar).Value = txtTopic.Text.Trim();
com.Parameters.Add("@Description", SqlDbType.NVarChar).Value = txtDescription.Text.Trim();
com.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = txtUserName.Text.Trim();
com.Parameters.Add("@RecordDate", SqlDbType.DateTime).Value = DateTime.Now;
com.Parameters.Add("@ViewCount", SqlDbType.Int).Value = 1;
com.Parameters.Add("@ReplyCount", SqlDbType.Int).Value = 0;
com.Parameters.Add("@IP", SqlDbType.NVarChar).Value = Request.ServerVariables["REMOTE_ADDR"];
com.ExecuteNonQuery();
Conn.Close();
Response.Redirect("Default.aspx");
}
protected void cmd_Click(object sender, EventArgs e)
{
txtTopic.Text = "";
txtDescription.Text = "";
txtUserName.Text = "";
}
}
Tag : ASP.NET, Ms SQL Server 2008, C#
|
ประวัติการแก้ไข 2011-03-01 20:33:22
|
|
|
|
|
Date :
2011-03-01 20:20:55 |
By :
lamusa |
View :
1458 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
StringBuilder.Remove()
|
|
|
|
|
Date :
2011-03-02 08:44:28 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|