【发布时间】:2020-10-23 17:38:36
【问题描述】:
在我的 WEB 文件夹中,我有文件 ʻaspx.cs` 和以下代码:
using System;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication1.bibliotecario
{
public partial class adicionar_livros : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\samue\source\repos\WebApplication1\WebApplication1\App_Data\gerenciamentoBiblioteca.mdf;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == System.Data.ConnectionState.Open)
{
con.Close();
}
con.Open();
}
protected void b1_Click(object sender, EventArgs e)
{
string books_image_name = Class1.GetRandomPassword(10);
string path = "";
f1.SaveAs(Request.PhysicalApplicationPath + "/bibliotecario/imagens_livros/" + f1.FileName.ToString());
path = "imagens_livros/"+f1.FileName.ToString();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into livros values('"+ titulolivros.Text +"','"+ path.ToString() + "','"+ nomeautor.Text + "','" + isbn.Text + "','" + qtd.Text + "')";
cmd.ExecuteNonQuery();
msg.Style.Add("display", "block");
}
}
}
我在 Asp.net 文件夹 App_Code 文件夹中有一个名为 Class1 的.cs 类。但是,每次我调用 Class1 时,它都无法识别,没有选项可以将其放入我的代码中,我该如何访问它?
这是我的Class1.cs
using System;
namespace WebApplication1.App_Code
{
public class Class1
{
public static string GetRandomPassword(int length)
{
char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
string password = string.Empty;
Random random = new Random();
for (int i = 0; i < length; i++)
{
int x = random.Next(1, chars.Length);
//For avoiding Repetation of Characters
if (!password.Contains(chars.GetValue(x).ToString()))
password += chars.GetValue(x);
else
i = i - 1;
}
return password;
}
}
}
【问题讨论】:
-
您需要使用
using包含您的课程。这回答了你的问题了吗? Import a class in ASP.NET -
string books_image_name = App_Code.Class1.GetRandomPassword(10); -
我怎样才能把它放在我的代码中?
using Class1;在我的apsx文件中? -
当我输入
using WebApplication1.App_Code;时,据说没有命名空间,当我按照 VDWWD 所说的做时,似乎App_Code在当前上下文中不存在 -
右键单击class1,并确保在属性表中将其设置为编译。
标签: c# asp.net visual-studio