我有这个方法,必须将一些数据插入我的数据库
public Zakaznik_Mapper()
{
public bool Insert(int ID_zakaznik, String Name, String LastName, String Login, String Password, int Number, String Email, String Adress)
{
try
{
conn.Open();
tran = conn.BeginTransaction(System.Data.IsolationLevel.Serializable);
SqlCommand insertCommand = new SqlCommand("INSERT INTO Zakaznik VALUES (@ID_zakaznik, @Name, @LastName, @Login, @Password, @Number, @Email, @Adress );", conn, tran);
insertCommand.Parameters.Add(new SqlParameter("@ID_zakaznik", SqlDbType.Int) { Value = ID_zakaznik });
insertCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar) { Value = Name });
insertCommand.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar) { Value = LastName });
insertCommand.Parameters.Add(new SqlParameter("@Login", SqlDbType.VarChar) { Value = Login });
insertCommand.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar) { Value = Password });
insertCommand.Parameters.Add(new SqlParameter("@Number", SqlDbType.Int) { Value = Number });
insertCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar) { Value = Email });
insertCommand.Parameters.Add(new SqlParameter("@Adress", SqlDbType.VarChar) { Value = Adress });
insertCommand.ExecuteNonQuery();
tran.Commit();
conn.Close();
return true;
}
catch (SqlException e)
{
tran.Rollback();
conn.Close();
Console.WriteLine(e);
return false;
}
}
我希望从UlozZakaznik()调用方法Insert,但我得到错误类型“方法没有重载'插入'需要0个参数”如果有人可以帮忙,我会很高兴:)
public void UlozZakaznik()
{
DataTable data_zakaznik = new DataTable("Zakazka");
data_zakaznik.Columns.Add("ID_Zakaznik");
data_zakaznik.Columns.Add("Name");
data_zakaznik.Columns.Add("Lastname");
data_zakaznik.Columns.Add("Login");
data_zakaznik.Columns.Add("Password");
data_zakaznik.Columns.Add("Number");
data_zakaznik.Columns.Add("Email");
data_zakaznik.Columns.Add("Adress");
Zakaznik_Mapper zakaznik = new Zakaznik_Mapper();
Zakaznik zakaznik1 = new Zakaznik();
zakaznik1.ID_zakaznik = Convert.ToInt32(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value);
zakaznik1.Name = Convert.ToString(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value);
zakaznik1.LastName = Convert.ToString(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value);
zakaznik1.Login = Convert.ToString(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[3].Value);
zakaznik1.Password = Convert.ToString(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[4].Value);
zakaznik1.Number = Convert.ToInt32(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[5].Value);
zakaznik1.Email = Convert.ToString(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[6].Value);
zakaznik1.Adress = Convert.ToString(dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[7].Value);
if (dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[0].Value != null
&& dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[1].Value != null
&& dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[2].Value != null
&& dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[3].Value != null
&& dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[4].Value != null
&& dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[5].Value != null
&& dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[6].Value != null
&& dataGridView1.Rows[dataGridView1.RowCount - 2].Cells[7].Value != null
)
{
data_zakaznik.Rows.Add(zakaznik1.ID_zakaznik, zakaznik1.Name, zakaznik1.LastName, zakaznik1.Login, zakaznik1.Password, zakaznik1.Number, zakaznik1.Email, zakaznik1.Adress);
zakaznik.Insert();//problem is here "Error type: No overload for method 'Insert' takes 0 arguments"
}
}
答案 0 :(得分:4)
你需要为你的功能提供参数。
下面:
public bool Insert(int ID_zakaznik,
String Name,
String LastName,
String Login,
String Password,
int Number,
String Email,
String Adress)
// ^^^^^^^^^^^^^^ arguments
该函数希望您将其传递给它。
所以这个:
zakaznik.Insert();
应该是这样的:
zakaznik.Insert(id_here, name_here, last_name_here, ..., ..., ...); // etc
答案 1 :(得分:1)
错误告诉你你到底做错了什么。您的Insert()方法需要8个参数,但是您提供的是0。
答案 2 :(得分:1)
错误很明显,你不能只是调用它而不传递数据,它需要一定数量的参数,所以你必须传递它们。
这是为了工作还是学校或只是一个爱好?如果是工作/学校,你应该联系任何负责你的人,并要求你提供一些深入的建议,因为你真的需要坐几个小时来学习它。
然而,在您的具体情况下,我假设您正在寻找的是替换
zakaznik.Insert();
与
zakaznik.Insert(zakaznik1.ID_zakaznik, zakaznik1.Name, zakaznik1.LastName, zakaznik1.Login, zakaznik1.Password, zakaznik1.Number, zakaznik1.Email, zakaznik1.Adress);