<HTML>
<HEAD>
<title>无标题文档</title><% @ Page Language="C#" Debug="true" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<% @ Import Namespace="System.IO" %>
<script language="C#" runat="server">
SqlConnection MyConnection;
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
MyConnection =new SqlConnection("server=yang;database=xjhome;uid=sa;pwd=;");
MyConnection.Open();
SqlDataAdapter mydata=new SqlDataAdapter("SELECT id,typename FROM newclass ",MyConnection);
DataSet ds= new DataSet();
mydata.Fill(ds,"newclass");
DropDownList2.DataSource = ds.Tables["newclass"].DefaultView;
DropDownList2.DataTextField = "typename";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
CheckBox1.Checked=false;
}
} public void Button1_Click(object Source, EventArgs e)
{ if ((biaoti.Text=="")||(neirong.Text=="")||(zuozhe.Text==""))
{
Label1.Text="标题、内容、作者等不能为空!";
return;
}
if (biaoti.Text.Length>=50)
{
Label1.Text="你的标题太长了!";
return;
}
if (File1.PostedFile.ContentLength>153600)
{
Span1.Text="上传的文件不能超过70kb";
return;
}
string filepath;
if (File1.PostedFile != null)
{
filepath= Server.MapPath(".")+"/news/file/"+Path.GetFileName (File1.PostedFile.FileName);
if (File.Exists(filepath))
{
Span1.Text="上传文件重名,请改名后再上传!";
return;
}
if(File1.PostedFile != null)
try
{
File1.PostedFile.SaveAs(filepath);
SqlConnection MyConnection = new SqlConnection("server=yang;database=xjhome;uid=sa;pwd=;");
SqlCommand myCommand = new SqlCommand("insert into news (biaoti, neirong,zuozhe,shijian,click,img,typeid) values ('" + biaoti.Text.ToString() + "', '" + neirong.Text.ToString() + "','" + zuozhe.Text.ToString() + "','" + DateTime.Now.ToString() + "',0,'" + Path.GetFileName(File1.PostedFile.FileName) + "','" + DropDownList2.SelectedItem.Value+ "')" , MyConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
//File1.PostedFile="";
}
catch (Exception exc)
{
Span1.Text = "保存文件时出错<b>" + filepath + "</b><br>";
}
}
else
{
SqlConnection MyConnection = new SqlConnection("server=yang;database=xjhome;uid=sa;pwd=;");
SqlCommand myCommand = new SqlCommand("insert into news (biaoti, neirong,zuozhe,shijian,click,img,typeid) values ('" + biaoti.Text.ToString() + "', '" + neirong.Text.ToString() + "','" + zuozhe.Text.ToString() + "','" + DateTime.Now.ToString() + "',0,'','" + DropDownList2.SelectedItem.Value+ "')" , MyConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
}
public void reset_Click(Object sender,EventArgs e)
{
biaoti.Text="";
neirong.Text="";
zuozhe.Text="";
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="Style.css" type="text/css">
</HEAD>
<body bgcolor="#99ccff" text="#000000" background="back.jpg">
<form enctype="multipart/form-data" runat="server">
<table width="50%" border="0" align="center">
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<div align="center"><b>管 理 页 面</b></div>
</td>
</tr>
<tr>
<td colspan="2">
类别:
<asp:DropDownList id="DropDownList2" runat="server" />
是否头条:
<asp:CheckBox id="CheckBox1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<div align="left"></div>
<div align="center">
<asp:Label id="Label1" ForeColor="Red" runat="server" />
</div>
</td>
</tr>
<tr>
<td colspan="2">标题:<asp:textbox id="biaoti" runat="server" /></td>
</tr>
<tr>
<td colspan="2">内容:<asp:textbox id="neirong" TextMode="MultiLine" Height="300px" Width="300px" runat="server" /></td>
</tr>
<tr>
<td colspan="2">作者:<asp:textbox id="zuozhe" runat="server" /></td>
</tr>
<tr>
<td width="73%">图片: <input id="File1" type="file" Accept="Image/*" runat="server">
</td>
<td width="27%">
</td>
</tr>
<tr>
<td width="73%">
<asp:label id="Span1" runat="server" />
</td>
<td width="27%"> </td>
</tr>
<tr>
<td height="15" colspan="2"></td>
</tr>
<tr>
<td colspan="2"><input type="button" id="Button1" value="上载" OnServerClick="Button1_Click" runat="server">
<asp:button id="button2" text="取消" OnClick="reset_Click" runat="server" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
</body>
</HTML>
这个程序图片饼没有存到数据库种
图片预览:得到所选的文件名,放到img种
祝你好运
<HEAD>
<title>无标题文档</title><% @ Page Language="C#" Debug="true" %>
<% @ Import Namespace="System.Data" %>
<% @ Import Namespace="System.Data.SqlClient" %>
<% @ Import Namespace="System.IO" %>
<script language="C#" runat="server">
SqlConnection MyConnection;
void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
MyConnection =new SqlConnection("server=yang;database=xjhome;uid=sa;pwd=;");
MyConnection.Open();
SqlDataAdapter mydata=new SqlDataAdapter("SELECT id,typename FROM newclass ",MyConnection);
DataSet ds= new DataSet();
mydata.Fill(ds,"newclass");
DropDownList2.DataSource = ds.Tables["newclass"].DefaultView;
DropDownList2.DataTextField = "typename";
DropDownList2.DataValueField = "id";
DropDownList2.DataBind();
CheckBox1.Checked=false;
}
} public void Button1_Click(object Source, EventArgs e)
{ if ((biaoti.Text=="")||(neirong.Text=="")||(zuozhe.Text==""))
{
Label1.Text="标题、内容、作者等不能为空!";
return;
}
if (biaoti.Text.Length>=50)
{
Label1.Text="你的标题太长了!";
return;
}
if (File1.PostedFile.ContentLength>153600)
{
Span1.Text="上传的文件不能超过70kb";
return;
}
string filepath;
if (File1.PostedFile != null)
{
filepath= Server.MapPath(".")+"/news/file/"+Path.GetFileName (File1.PostedFile.FileName);
if (File.Exists(filepath))
{
Span1.Text="上传文件重名,请改名后再上传!";
return;
}
if(File1.PostedFile != null)
try
{
File1.PostedFile.SaveAs(filepath);
SqlConnection MyConnection = new SqlConnection("server=yang;database=xjhome;uid=sa;pwd=;");
SqlCommand myCommand = new SqlCommand("insert into news (biaoti, neirong,zuozhe,shijian,click,img,typeid) values ('" + biaoti.Text.ToString() + "', '" + neirong.Text.ToString() + "','" + zuozhe.Text.ToString() + "','" + DateTime.Now.ToString() + "',0,'" + Path.GetFileName(File1.PostedFile.FileName) + "','" + DropDownList2.SelectedItem.Value+ "')" , MyConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
//File1.PostedFile="";
}
catch (Exception exc)
{
Span1.Text = "保存文件时出错<b>" + filepath + "</b><br>";
}
}
else
{
SqlConnection MyConnection = new SqlConnection("server=yang;database=xjhome;uid=sa;pwd=;");
SqlCommand myCommand = new SqlCommand("insert into news (biaoti, neirong,zuozhe,shijian,click,img,typeid) values ('" + biaoti.Text.ToString() + "', '" + neirong.Text.ToString() + "','" + zuozhe.Text.ToString() + "','" + DateTime.Now.ToString() + "',0,'','" + DropDownList2.SelectedItem.Value+ "')" , MyConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
}
public void reset_Click(Object sender,EventArgs e)
{
biaoti.Text="";
neirong.Text="";
zuozhe.Text="";
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="Style.css" type="text/css">
</HEAD>
<body bgcolor="#99ccff" text="#000000" background="back.jpg">
<form enctype="multipart/form-data" runat="server">
<table width="50%" border="0" align="center">
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
<div align="center"><b>管 理 页 面</b></div>
</td>
</tr>
<tr>
<td colspan="2">
类别:
<asp:DropDownList id="DropDownList2" runat="server" />
是否头条:
<asp:CheckBox id="CheckBox1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<div align="left"></div>
<div align="center">
<asp:Label id="Label1" ForeColor="Red" runat="server" />
</div>
</td>
</tr>
<tr>
<td colspan="2">标题:<asp:textbox id="biaoti" runat="server" /></td>
</tr>
<tr>
<td colspan="2">内容:<asp:textbox id="neirong" TextMode="MultiLine" Height="300px" Width="300px" runat="server" /></td>
</tr>
<tr>
<td colspan="2">作者:<asp:textbox id="zuozhe" runat="server" /></td>
</tr>
<tr>
<td width="73%">图片: <input id="File1" type="file" Accept="Image/*" runat="server">
</td>
<td width="27%">
</td>
</tr>
<tr>
<td width="73%">
<asp:label id="Span1" runat="server" />
</td>
<td width="27%"> </td>
</tr>
<tr>
<td height="15" colspan="2"></td>
</tr>
<tr>
<td colspan="2"><input type="button" id="Button1" value="上载" OnServerClick="Button1_Click" runat="server">
<asp:button id="button2" text="取消" OnClick="reset_Click" runat="server" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</form>
</body>
</HTML>
这个程序图片饼没有存到数据库种
图片预览:得到所选的文件名,放到img种
祝你好运
解决方案 »
- 怎么把我的信息发送到我的邮箱呢
- 页面如何自动下载OCX控件并安装?
- 请教用ASP.net做网页问题
- 如何使用ADO。NET返回SQL中的@@INDENTITY变量
- 可以跳转到另一个项目文件的网页吗?
- ajax绑定选项卡下的GridView出问题??在线=,谢谢
- 怎样在Web页上时时显示当前服务器的系统时间
- 关于session的问题
- .net多人开发,如何来保证安全性,即别人不能修改你编的代码?
- 斑竹、微软技术专家、各位大虾:在ASP.NET中怎样实现浮动层的效果,具体就是怎样让Calendar在需要时浮动显示出来,而不要推动下面的对象
- 一个webform真的只能有一个<form runat="server">么?
- 日期的正则表达式
我把C#翻译成VB.net后出现错误:异常详细信息: System.InvalidCastException: 对象必须实现 IConvertible。源错误: 行 71: Dim numRowsAffected As Integer
行 72: numRowsAffected = myCommand.ExecuteNonQuery()
行 73: objConnection.Close()
行 74: End Sub
行 75:
源文件: \\Sz1860-report\wwwroot\Web\news\testadd.aspx.vb 行: 73