PDA

View Full Version : دسترسی به فیلد داخل بانک



mehran63
یک شنبه 22 اسفند 1389, 18:32 عصر
سلام
کسی میدونه چه طور میتونم به یک فیلد در بانکم از طریق C# دسترسی داشته باشم
مثلا بگم اگه فیلد code برابر با فلان مقدار بود textcode.text رو خالی کن
اگه یه کد گذاشتید ممنون میشم

mehranmsba
یک شنبه 22 اسفند 1389, 18:46 عصر
یک راه ساده دارد:
اگر دیتابیس شما درون یک Dataset قرار گرفته باشد:

if(yourdataset.yourtablename[row][coloumn].ToString()==text1.Text)
text1.Text=string.Empty;

mehran63
یک شنبه 22 اسفند 1389, 19:54 عصر
با این روش که نشد اسم جدولم tbuy و اسم فیلد مورد نظرم code هستش

mehranmsba
یک شنبه 22 اسفند 1389, 20:52 عصر
این موارد را بررسی کنید:
1-آیا دیتاست مقدارگیری کرده است؟ مثلا با Fill پر شده باشد/
2-بستگی به نوع اتصال کد زیر را بررسی کنید:


testDataSet1.Tables["tbuy"].Rows[shmora satr]["code"]

mehran63
دوشنبه 23 اسفند 1389, 17:52 عصر
پایین کل کد را میزارم لطفا یکی بگه چه باید کرد


public partial class Form1 : Form
{
private const string _CommandText =
"select * from tbuy order by code";

private const string _ConnectionString =
"server=localhost;database=for_work;integrated security=true;";

SqlConnection objConnection;
SqlDataAdapter objDataAdapter;
DataSet objDataSet;
DataView objDataView;
CurrencyManager objCurrencyManager;

public Form1()
{
objConnection = new SqlConnection(_ConnectionString);
objDataAdapter = new SqlDataAdapter(_CommandText, objConnection);

InitializeComponent();
}

private void FillDataSetAndView()
{
objDataSet = new DataSet();
objDataAdapter.Fill(objDataSet, "tbuy");
objDataView = new DataView(objDataSet.Tables["tbuy"]);
objCurrencyManager = (CurrencyManager)
(this.BindingContext[objDataView]);
}// end method FillDataSetAndView

private void BindFields()
{
txtCode.DataBindings.Clear();
txtCount.DataBindings.Clear();
txtCountry.DataBindings.Clear();
txtDate.DataBindings.Clear();
txtDescription.DataBindings.Clear();
txtManufacturer.DataBindings.Clear();
txtModel.DataBindings.Clear();
txtName.DataBindings.Clear();
txtPrice.DataBindings.Clear();
txtSN.DataBindings.Clear();

txtCode.DataBindings.Add("text", objDataView, "code");
txtCount.DataBindings.Add("text", objDataView, "count");
txtCountry.DataBindings.Add("text", objDataView, "country");
txtDate.DataBindings.Add("text", objDataView, "date");
txtDescription.DataBindings.Add("text", objDataView, "description");
txtManufacturer.DataBindings.Add("text", objDataView, "manufacturer");
txtModel.DataBindings.Add("text", objDataView, "model");
txtName.DataBindings.Add("text", objDataView, "name");
txtPrice.DataBindings.Add("text", objDataView, "price");
txtSN.DataBindings.Add("text", objDataView, "sn");

toolStripStatusLabel1.Text = "Redy";
}// end method BindFields

private void ShowPosition()
{
try
{
txtPrice.Text = decimal.Parse(txtPrice.Text).ToString("##0.00");
}// end try

catch (System.Exception e)
{
txtPrice.Text = "0";
txtPrice.Text = decimal.Parse(txtPrice.Text).ToString("##0.00");
}// end catch

txtRecordPosition.Text = (objCurrencyManager.Position + 1) +
" Of " + objCurrencyManager.Count;
}// end method ShowPosition

private void Form1_Load(object sender, EventArgs e)
{
cboField.Items.Add("code");
cboField.Items.Add("name");
cboField.Items.Add("model");
cboField.Items.Add("manufacturer");
cboField.Items.Add("sn");
cboField.Items.Add("country");
cboField.Items.Add("price");
cboField.Items.Add("count");
cboField.Items.Add("date");
cboField.Items.Add("description");

cboField.SelectedIndex = 0;

FillDataSetAndView();
BindFields();
ShowPosition();
}

private void btnMoveFirst_Click(object sender, EventArgs e)
{
BindFields();
objCurrencyManager.Position = 0;
ShowPosition();
}// end method btnMoveFirst_Click

private void btnMovePrevious_Click(object sender, EventArgs e)
{
BindFields();
objCurrencyManager.Position -= 1;
ShowPosition();
}// end method btnMovePrevious_Click

private void btnMoveNext_Click(object sender, EventArgs e)
{
BindFields();
objCurrencyManager.Position += 1;
ShowPosition();
}// end method btnMoveNext_Click

private void btnMoveLast_Click(object sender, EventArgs e)
{
BindFields();
objCurrencyManager.Position =
objCurrencyManager.Count - 1;
ShowPosition();
}// end method btnMoveLast_Click

private void btnPerformSort_Click(object sender, EventArgs e)
{
switch (cboField.SelectedIndex)
{
case 0:
objDataView.Sort = "code";
break;
case 1:
objDataView.Sort = "name";
break;
case 2:
objDataView.Sort = "model";
break;
case 3:
objDataView.Sort = "manufacturer";
break;
case 4:
objDataView.Sort = "sn";
break;
case 5:
objDataView.Sort = "country";
break;
case 6:
objDataView.Sort = "price";
break;
case 7:
objDataView.Sort = "count";
break;
case 8:
objDataView.Sort = "date";
break;
case 9:
objDataView.Sort = "description";
break;
}// end switch

btnMoveFirst_Click(null, null);

toolStripStatusLabel1.Text = "Records Sorted";
}// end method btnPerformSort_Click

private void btnPerformSearch_Click(object sender, EventArgs e)
{
int intPosition;

btnPerformSort_Click(null, null);
if ((cboField.SelectedIndex == 0) ||
(cboField.SelectedIndex == 4) ||
(cboField.SelectedIndex == 6) ||
(cboField.SelectedIndex == 7))
{
intPosition = objDataView.Find
(decimal.Parse(txtSearchCriteria.Text));
}// end if
else
{
intPosition = objDataView.Find(txtSearchCriteria.Text);
}// end else

if (intPosition == -1)
{
toolStripStatusLabel1.Text = "Record Not Found";
}// end if
else
{
toolStripStatusLabel1.Text = "Record found";
objCurrencyManager.Position = intPosition;
}// end else

ShowPosition();
}// end method btnPerformSearch_Click

private void btnNew_Click(object sender, EventArgs e)
{

txtCode.Text = "";
txtCount.Text = "";
txtCountry.Text = "";
txtDate.Text = "";
txtDescription.Text = "";
txtManufacturer.Text = "";
txtModel.Text = "";
txtName.Text = "";
txtPrice.Text = "";
txtSN.Text = "";
}// end method btnAdd_Click

private void btnAdd_Click(object sender, EventArgs e)
{
int intPosition;
intPosition = objCurrencyManager.Position;
SqlCommand objCommand=new SqlCommand();
objCommand.Connection = objConnection;
objCommand.CommandText = "insert into tbuy (code,name,model," +
"manufacturer,sn,country,price,count,date,descripti on)" +
"values(@code,@name,@model,@manufacturer,@sn,@count ry," +
"@price,@count,@date,@description)";
objCommand.Parameters.AddWithValue("@code", txtCode.Text);
objCommand.Parameters.AddWithValue("@name", txtName.Text);
objCommand.Parameters.AddWithValue("@model", txtModel.Text);
objCommand.Parameters.AddWithValue("@manufacturer",
txtManufacturer.Text);
objCommand.Parameters.AddWithValue("@sn",txtSN.Text);
objCommand.Parameters.AddWithValue("@country", txtCountry.Text);
objCommand.Parameters.AddWithValue("@price",txtPrice.Text);
objCommand.Parameters.AddWithValue("@count",txtCount.Text);
objCommand.Parameters.AddWithValue("@date", txtDate.Text);
objCommand.Parameters.AddWithValue("@description",
txtDescription.Text);

objConnection.Open();
try
{
int i=objCommand.ExecuteNonQuery();
if (i == 1)
MessageBox.Show("اطلاعات شما با موفقیت ثبت شد");
}// end try
catch (SqlException sqlExceptionErr)
{
if (sqlExceptionErr.Number == 2627)
MessageBox.Show("اين کد قبلا ثبت شده");
else
MessageBox.Show(sqlExceptionErr.Message);
}// end catch

objConnection.Close();

FillDataSetAndView();
BindFields();

objCurrencyManager.Position = intPosition;
ShowPosition();

toolStripStatusLabel1.Text = "Record Added";

}// end btnAdd_Click

private void btnUpdate_Click(object sender, EventArgs e)
{
int intposition;
string Oldc;
Oldc = txtCode.Text;
SqlCommand objCommand = new SqlCommand();
intposition = objCurrencyManager.Position;
objCommand.Connection = objConnection;
objCommand.CommandText = "update tbuy set code=@NewCode,name=@name," +
"model=@model,manufacturer=@manufacturer,sn=@sn," +
"country=@country,price=@price,count=@count," +
"date=@date,description=@description where code=@Oldc";
objCommand.CommandType = CommandType.Text;
objCommand.Parameters.AddWithValue("@Oldc", txtCode.Text);
objCommand.Parameters.AddWithValue("@NewCode", txtCode.Text);
objCommand.Parameters.AddWithValue("@Code", txtCode.Text);
objCommand.Parameters.AddWithValue("@name", txtName.Text);
objCommand.Parameters.AddWithValue("@model", txtModel.Text);
objCommand.Parameters.AddWithValue("@manufacturer",
txtManufacturer.Text);
objCommand.Parameters.AddWithValue("@sn",txtSN.Text);
objCommand.Parameters.AddWithValue("@country", txtCountry.Text);
objCommand.Parameters.AddWithValue("@price",txtPrice.Text);
objCommand.Parameters.AddWithValue("@count",txtCount.Text);
objCommand.Parameters.AddWithValue("@date", txtDate.Text);
objCommand.Parameters.AddWithValue("@description",
txtDescription.Text);

objConnection.Open();
try
{
int i = objCommand.ExecuteNonQuery();
if (i == 1)
MessageBox.Show("اطلاعات شما با موفقیت ثبت شد");
}// end try
catch (SqlException sqlExceptionErr)
{
if (sqlExceptionErr.Number == 2627)
MessageBox.Show("اين کد قبلا ثبت شده");
else
MessageBox.Show(sqlExceptionErr.Message);
}// end catch
objConnection.Close();

FillDataSetAndView();
BindFields();

objCurrencyManager.Position = intposition;
ShowPosition();

toolStripStatusLabel1.Text = "Record Updated";

}// end btnUpdate_Click

private void btnDelete_Click(object sender, EventArgs e)
{
int intPosition;
SqlCommand objCommand = new SqlCommand();

intPosition = this.BindingContext[objDataView].Position - 1;
if (intPosition < 0)
intPosition = 0;

objCommand.Connection = objConnection;
objCommand.CommandText = "delete from tbuy where code=@code";
objCommand.Parameters.AddWithValue("@code",
this.BindingContext[objDataView, "code"].Current);

objConnection.Open();
objCommand.ExecuteNonQuery();
objCommand.Clone();

FillDataSetAndView();
BindFields();

this.BindingContext[objDataView].Position = intPosition;

ShowPosition();

toolStripStatusLabel1.Text = "Record Deleted";
}
}

mehran63
چهارشنبه 25 اسفند 1389, 20:11 عصر
لطفا یکی یه راه حلی بده

mehran63
شنبه 28 اسفند 1389, 09:23 صبح
از مدیران انجمن میخواهم لطفا کمکم کنن