主题:[讨论]图片名存储到数据库
为什么我这样子存储到数据库中的图片名只有images/ 是哪里错了呢?name是全局变量,但是好像name没有值(也就是颜色字体处) 但是在上传时 已经有图片路径了,大家帮我看看啊!谢谢拉!!!!!
[em2]
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class register : System.Web.UI.Page
{
public string name;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button5_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName != "")
{
Image1.Visible = false;
string fullname = this.FileUpload1.FileName;
FileInfo f = new FileInfo(fullname);
name = f.Name;
string type = f.Extension;
if (".gif" == type || ".jpg" == type || ".GIF" == type || ".JPG" == type)
{
string savapath = Server.MapPath("images\\" + name);
this.FileUpload1.PostedFile.SaveAs(savapath);
this.Image1.Visible = true;
this.Image1.ImageUrl = savapath;
}
else
{
Response.Write("<script>alert('请选择GIF和JPG格式图片')</script>");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
con.Open();
string sql = "select username from register where username=@username";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.Add("@username",SqlDbType.NChar);
cmd.Parameters["@username"].Value = this.TextBox1.Text.ToString();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.Write("<script>alert('用户名存在!')</script>");
return;
}
dr.Close();
string sqltext = "insert into register(username,sex,age,password,repassword,email,question,answer,telephone,photo) values(@username,@sex,@age,@password,@repassword,@email,@question,@answer,@telephone,@photo)";
SqlCommand scmd = new SqlCommand(sqltext,con);
scmd.Parameters.Add("@username",SqlDbType.NChar,12);
scmd.Parameters["@username"].Value = this.TextBox1.Text.ToString();
scmd.Parameters.Add("@sex",SqlDbType.NChar,2);
scmd.Parameters["@sex"].Value = this.RadioButtonList1.SelectedValue.ToString();
scmd.Parameters.Add("@age",SqlDbType.Int);
scmd.Parameters["@age"].Value = int.Parse(this.TextBox4.Text);
scmd.Parameters.Add("@password",SqlDbType.NChar,12);
scmd.Parameters["@password"].Value = this.TextBox2.Text.ToString();
scmd.Parameters.Add("@repassword", SqlDbType.NChar, 12);
scmd.Parameters["@repassword"].Value = this.TextBox3.Text.ToString();
scmd.Parameters.Add("@email",SqlDbType.NChar,20);
scmd.Parameters["@email"].Value = this.TextBox5.Text.ToString();
scmd.Parameters.Add("@question",SqlDbType.NChar,1000);
scmd.Parameters["@question"].Value = this.TextBox7.Text.ToString();
scmd.Parameters.Add("@answer",SqlDbType.NChar,1000);
scmd.Parameters["@answer"].Value = this.TextBox8.Text.ToString();
scmd.Parameters.Add("@telephone",SqlDbType.NChar,11);
scmd.Parameters["@telephone"].Value = this.TextBox6.Text.ToString();
scmd.Parameters.Add("@photo",SqlDbType.VarChar,10);
[color=008000] scmd.Parameters["@photo"].Value ="images/"+name;[/color]
try
{
scmd.ExecuteNonQuery();
Response.Write("<script>alert('注册成功!')</script>");
}
catch (Exception error)
{
throw new System.Exception(error.Message);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
string sql = "select count(*) from register where username = '" + TextBox1.Text + "'";
con.Open();
SqlCommand cmd1=new SqlCommand(sql,con);
int count = Convert.ToInt32(cmd1.ExecuteScalar());
if (count > 0)
{
Response.Write("<script>alert('用户名不可用!');</script>");
}
else
{
Response.Write("<script>alert('用户名可以使用!');</script>");
}
con.Close();
}
}
[em2]
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class register : System.Web.UI.Page
{
public string name;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button5_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName != "")
{
Image1.Visible = false;
string fullname = this.FileUpload1.FileName;
FileInfo f = new FileInfo(fullname);
name = f.Name;
string type = f.Extension;
if (".gif" == type || ".jpg" == type || ".GIF" == type || ".JPG" == type)
{
string savapath = Server.MapPath("images\\" + name);
this.FileUpload1.PostedFile.SaveAs(savapath);
this.Image1.Visible = true;
this.Image1.ImageUrl = savapath;
}
else
{
Response.Write("<script>alert('请选择GIF和JPG格式图片')</script>");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
con.Open();
string sql = "select username from register where username=@username";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.Add("@username",SqlDbType.NChar);
cmd.Parameters["@username"].Value = this.TextBox1.Text.ToString();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.Write("<script>alert('用户名存在!')</script>");
return;
}
dr.Close();
string sqltext = "insert into register(username,sex,age,password,repassword,email,question,answer,telephone,photo) values(@username,@sex,@age,@password,@repassword,@email,@question,@answer,@telephone,@photo)";
SqlCommand scmd = new SqlCommand(sqltext,con);
scmd.Parameters.Add("@username",SqlDbType.NChar,12);
scmd.Parameters["@username"].Value = this.TextBox1.Text.ToString();
scmd.Parameters.Add("@sex",SqlDbType.NChar,2);
scmd.Parameters["@sex"].Value = this.RadioButtonList1.SelectedValue.ToString();
scmd.Parameters.Add("@age",SqlDbType.Int);
scmd.Parameters["@age"].Value = int.Parse(this.TextBox4.Text);
scmd.Parameters.Add("@password",SqlDbType.NChar,12);
scmd.Parameters["@password"].Value = this.TextBox2.Text.ToString();
scmd.Parameters.Add("@repassword", SqlDbType.NChar, 12);
scmd.Parameters["@repassword"].Value = this.TextBox3.Text.ToString();
scmd.Parameters.Add("@email",SqlDbType.NChar,20);
scmd.Parameters["@email"].Value = this.TextBox5.Text.ToString();
scmd.Parameters.Add("@question",SqlDbType.NChar,1000);
scmd.Parameters["@question"].Value = this.TextBox7.Text.ToString();
scmd.Parameters.Add("@answer",SqlDbType.NChar,1000);
scmd.Parameters["@answer"].Value = this.TextBox8.Text.ToString();
scmd.Parameters.Add("@telephone",SqlDbType.NChar,11);
scmd.Parameters["@telephone"].Value = this.TextBox6.Text.ToString();
scmd.Parameters.Add("@photo",SqlDbType.VarChar,10);
[color=008000] scmd.Parameters["@photo"].Value ="images/"+name;[/color]
try
{
scmd.ExecuteNonQuery();
Response.Write("<script>alert('注册成功!')</script>");
}
catch (Exception error)
{
throw new System.Exception(error.Message);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
string sql = "select count(*) from register where username = '" + TextBox1.Text + "'";
con.Open();
SqlCommand cmd1=new SqlCommand(sql,con);
int count = Convert.ToInt32(cmd1.ExecuteScalar());
if (count > 0)
{
Response.Write("<script>alert('用户名不可用!');</script>");
}
else
{
Response.Write("<script>alert('用户名可以使用!');</script>");
}
con.Close();
}
}