新人求指点:C#文件上传程序出错

flowerontheroad 2010-09-24 07:59:55
这是App_Code中创建的类FileDB.cs
问题都出在这段程序上。加红色为出错地方。
出错原因:Invalid token ’=’ in class, struct, or interface member declaration
Invalid token ’;’ in class, struct, or interface member declaration
Invalid token ’try’ in class, struct, or interface member declaration
Invalid token ’(’ in class, struct, or interface member declaration
Expected class,delegate,enum,interface,or struct
A namespace does not directly contain members such as fields or methods

using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Class1
/// </summary>
public class FileDB
{
public FileDB()
{}
public static string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
public SqlDataReader GetFiles();


SqlConnection myConnection=new SqlConnection(SQLCONNECTIONSTRING);
SqlConnection myCommand=new SqlCommand("Pr_GetFiles",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader dr=null;
try{ myConnection.Open();}
catch (Exception ex) { throw new Exception("数据库连接失败!",ex);}

try
{
dr=myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex){throw new Exception(ex.Message,ex);}
return(dr);
}
public int AddFile(String sFileName,String sFileUrl,String sFileKind)
{
SqlConnection myConnection=new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand=new SqlCommand("Pr_AddFile",myConnection);
SqlParameter parameterFileID=new SqlParameter("@FileID",SqlDbType.Int,4);
parameterFileID.Direction=ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterFileID);
try{myConnection.Open();}
catch(Exception ex){throw new Exception("数据库连接失败!",ex);}
try{myCommand.ExecuteNonQuery();}
catch(Exception ex){throw new Exception(ex.Message,ex);}
return(int)parameterFileID.Value;
}


}
//
// TODO: Add constructor logic here
//


}
...全文
98 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
flowerontheroad 2010-09-24
  • 打赏
  • 举报
回复
.......身边的也都是菜鸟。。。。
hchxxzx 2010-09-24
  • 打赏
  • 举报
回复
这还真是手把手教啊,同志.
先向你身边的人请教一下吧,这网上一来一去的,时间耗不起啊.
flowerontheroad 2010-09-24
  • 打赏
  • 举报
回复
改完这些之后,我的upFile.aspx.cs就出了问题
if (File.Exists(Server.MapPath(Request.ApplicationPath) + "\\UpLoads" + fileName) == false)
说是当前上下文中不存在名称File。

真是命途多舛啊
wuyq11 2010-09-24
  • 打赏
  • 举报
回复
SqlCommand MyCommand = new SqlCommand("", conn);
MyCommand.CommandType = CommandType.StoredProcedure;

MyCommand.ExecuteReader();
flowerontheroad 2010-09-24
  • 打赏
  • 举报
回复
还是有错。。。抑郁
1、无法将类型“System.Data.SqlClient.SqlCommand"隐式转换为“System.Data.SqlClient.SqlConnection"
2、“System.Data.SqlClient.SqlConnection"不包含“commandType"的定义,并且找不到可接受类型为“System.Data.SqlClient.SqlConnection"的第一个参数的扩展方法“commandType"(是否缺少 using 指令或程序集引用?)

3、、“System.Data.SqlClient.SqlConnection"不包含“ExecuteReader"的定义,并且找不到可接受类型为“System.Data.SqlClient.SqlConnection"的第一个参数的扩展方法“ExecuteReader"(是否缺少 using 指令或程序集引用?)


huangwenquan123 2010-09-24
  • 打赏
  • 举报
回复
你鼠标移到SqlDataReader会自动提示!
huangwenquan123 2010-09-24
  • 打赏
  • 举报
回复
添加命名空间using System.Data.SqlClient;
flowerontheroad 2010-09-24
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// Summary description for Class1
/// </summary>
public class FileDB
{
public FileDB()
{}
public static string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();
public SqlDataReader GetFiles()
{
using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ToString()))
{
conn.Open();
SqlConnection myCommand=new SqlCommand("Pr_GetFiles",myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader dr=null;
try{ myConnection.Open();}
catch (Exception ex) { throw new Exception("数据库连接失败!",ex);}

try
{
dr=myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex){throw new Exception(ex.Message,ex);}
return(dr);
}
}
public int AddFile(String sFileName,String sFileUrl,String sFileKind)
{
SqlConnection myConnection=new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand=new SqlCommand("Pr_AddFile",myConnection);
SqlParameter parameterFileID=new SqlParameter("@FileID",SqlDbType.Int,4);
parameterFileID.Direction=ParameterDirection.ReturnValue;
myCommand.Parameters.Add(parameterFileID);
try{myConnection.Open();}
catch(Exception ex){throw new Exception("数据库连接失败!",ex);}
try{myCommand.ExecuteNonQuery();}
catch(Exception ex){throw new Exception(ex.Message,ex);}
return(int)parameterFileID.Value;
}



//
// TODO: Add constructor logic here
//


}
改成这样之后报错说找不到类型或命名空间名称“SqlDataReader”(是否缺少 using 指令或程序集引用?)
flowerontheroad 2010-09-24
  • 打赏
  • 举报
回复
照你的改完之后,又发生以下问题了
1、应输入类型、命名空间定义或文件尾
2、Type or namespace definition or end-of-file expected
该怎么办呢???
wuyq11 2010-09-24
  • 打赏
  • 举报
回复
public SqlDataReader GetFiles()
{
using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ToString()))
{
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
...
}

...
}

wuyq11 2010-09-24
  • 打赏
  • 举报
回复
using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ToString()))
{
conn.Open();
SqlCommand MyCommand = new SqlCommand("MYSQL", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
...
}
检查连接字符串

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧