string Connection = "Data Source=.;Initial Catalog=text;User ID=sa;Password=123;";
public DataTable YaoPinSelectById(string Id)
{
using (SqlConnection sqlConnection = new SqlConnection(Connection))
{
//sql语句
string sql = "select * from yaopin where id like '%'+ @id + '%'";
//添加sql条件
SqlParameter[] sqlParameters = { new SqlParameter("@id", Id) };
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sqlConnection))
{
sqlDataAdapter.SelectCommand.Parameters.AddRange(sqlParameters);
//添加容器
DataTable dt = new DataTable();
//填满
sqlDataAdapter.Fill(dt);
if (dt.Rows.Count > 0)
{
return dt;
}
else
{
return null;
}
}
}
}
//添加
public int YaoPinAdd(YaoPinModel yaoPin)
{
using (SqlConnection sqlConnection = new SqlConnection(Connection))
{
string sql = "INSERT INTO yaopin (id, name, leixin, jiliang, danwei) VALUES (@id, @name, @leixin, @jiliang, @danwei)";
SqlParameter[] sqlParameters =
{
new SqlParameter("@id", yaoPin.Id),
new SqlParameter("@name", yaoPin.Name),
new SqlParameter("@leixin", yaoPin.LeiXin),
new SqlParameter("@jiliang", yaoPin.JiLiang),
new SqlParameter("@danwei", yaoPin.DanWei)
};
using (SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection))
{
sqlCommand.Parameters.AddRange(sqlParameters);
sqlConnection.Open();
int i = sqlCommand.ExecuteNonQuery();
return i;
}
}
}
public DataTable YaopinSelectById(string Id)
{
Yaodal yaodal = new Yaodal();
DataTable dt = new DataTable();
dt = yaodal.YaoPinSelectById(Id);
return dt;
}
public bool YaopinAdd(YaoPinModel yaoPin)
{
Yaodal yaodal = new Yaodal();
int i = yaodal.YaoPinAdd(yaoPin);
if (i > 0)
{
return true;
}
else
{
return false;
}
}
private void btselect_Click(object sender, EventArgs e)
{
string Id = Idtext.Text;
Yaobll yaobll = new Yaobll();
YaoPinView.DataSource = yaobll.YaopinSelectById(Id);
}
//添加
private void Add_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("id不能为空");
return;
}
YaoPinModel yaoPinModel = new YaoPinModel {
#region 信息
Id = textBox1.Text,
Name = textBox2.Text,
LeiXin = textBox3.Text,
JiLiang = textBox4.Text,
DanWei = textBox5.Text,
#endregion
};
Yaobll yaobll = new Yaobll();
bool b = yaobll.YaopinAdd(yaoPinModel);
if (b)
{
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("添加失败");
}
}
https://aigptx.top/