का उपयोग कर SQL सर्वर में एकाधिक रिकॉर्ड्स कैसे स्टोर करें I C# का उपयोग कर DataGridView से SQL सर्वर डेटाबेस तालिका में 5 रिकॉर्ड्स डालने का प्रयास कर रहा हूं।डेटाग्रिड व्यू
मेरे कोड से यह कई रिकॉर्डों का इनपुट लेता है लेकिन डेटाबेस में केवल पहला रिकॉर्ड डालता है। क्या कोई मुझे सहेजने वाले बटन के एक क्लिक से डेटाबेस में 5 रिकॉर्ड सहेजने में मदद कर सकता है? for (int i = 0; i < dataGridView1.Rows.Count; i++)
को
DataSet ds = new DataSet();
SqlConnection cs = new SqlConnection(@"Data Source=DELL-PC;Initial Catalog=Image_DB;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
BindingSource Input = new BindingSource();
DataView dview = new DataView();
private void Form1_Load(object sender, EventArgs e)
{
//create a DataGridView Image Column
DataGridViewImageColumn dgvImage = new DataGridViewImageColumn();
//set a header test to DataGridView Image Column
dgvImage.HeaderText = "Images";
dgvImage.ImageLayout = DataGridViewImageCellLayout.Stretch;
DataGridViewTextBoxColumn dgvId = new DataGridViewTextBoxColumn();
dgvId.HeaderText = "ID";
DataGridViewTextBoxColumn dgvName = new DataGridViewTextBoxColumn();
dgvName.HeaderText = "Name";
dataGridView1.Columns.Add(dgvId);
dataGridView1.Columns.Add(dgvName);
dataGridView1.Columns.Add(dgvImage);
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.RowTemplate.Height = 120;
dataGridView1.AllowUserToAddRows = false;
}
// button add data to dataGridView
// insert image from pictureBox to dataGridView
private void btn_Add_Click(object sender, EventArgs e)
{
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
byte[] img = ms.ToArray();
dataGridView1.Rows.Add(txt_UserID.Text, txt_Name.Text, img);
}
// browse image in pictureBox1 Click
private void pictureBox1_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
if (opf.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(opf.FileName);
}
}
private void btn_Save_Click(object sender, EventArgs e)
{
for (int i = 5; i < dataGridView1.Rows.Count; i++)
{
string col1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col2 = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col3 = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string insert_sql = "INSERT INTO Input(UserID, UserName, PassImage) VALUES ('" + col1 + "','" + col2 + "','" + col3 + "')";
this.getcom(insert_sql);
}
MessageBox.Show("Record Added");
}
public SqlConnection GetSqlConnection() //connection function
{
string str_sqlcon = "Data Source=DELL-PC;Initial Catalog=Image_DB;Integrated Security=True";
SqlConnection mycon = new SqlConnection(str_sqlcon);
mycon.Open();
return mycon;
}
public void getcom(string sqlstr) //function for adding rows
{
SqlConnection sqlcon = this.GetSqlConnection(); // Watch out same string type as GetSQLConnection function
SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcom.ExecuteNonQuery();
sqlcom.Dispose();
sqlcon.Close();
sqlcon.Dispose();
}
बहुत बहुत धन्यवाद! यह काम किया .... – Sumi
क्या आप कृपया बता सकते हैं, मैं उपयोगकर्ता को 5 रिकॉर्ड रिकॉर्ड करने के लिए कैसे प्रतिबंधित कर सकता हूं अन्यथा कोई संदेश उत्पन्न होगा। – Sumi
लूप ** के लिए ** में परिवर्तन करें, -> 'के लिए (i = 0; i <6; i ++)' तो यह केवल 5 रिकॉर्ड डालेगा। यदि आप संदेश का उपयोग डेटाग्रिडव्यू क्लाइंट इवेंट्स को दिखाना चाहते हैं या ** ** अगर (डेटाग्रिड व्यू 1.रोस.count> 5) '। @ सुमी में तर्क का प्रयास करें। – Balaji