日期:2014-05-17  浏览次数:20498 次

新手数据库问题!
插不进数据,不知道为什么!!!
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        string t1 = this.TextBox1.Text;
        SqlCommand cmd = null;
        SqlConnection con = null;
        try
        {
            con = new SqlConnection("server=192.168.2.169;uid=sa;pwd=sa;database=NewTrafficManagement");
            string str = string.Format("insert into Areas(AreaName)values('{0}')", t1);
            cmd = new SqlCommand(str, con);
            con.Open();
            cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('ex.Message')</script>");
        }
        finally
        {
            if (cmd != null)
            {
                cmd.Dispose();
            }
            if (con != null)
            {
                con.Dispose();
            }
        }
    }
}