<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%">&nbsp;</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">&nbsp;</td>
</tr>
</table>
</form>
</body>
</HTML>
这个程序图片饼没有存到数据库种
图片预览:得到所选的文件名,放到img种
祝你好运

解决方案 »

  1.   

    shit,人家不是叫你用vb.net,你把人家的C#贴过来干甚么
      

  2.   

    我也想知道啊!高手快来啊!我也可另给分!
    我把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