載入A資料庫修改欄位裡面的值,但無法逐項修改此欄位值有人給我foreach這個KEY WORD但是無法達到目的,請問站內高手哪裡出錯了嗎?
附上CODING以便查看:
try
{
using (SqlConnection connAMHS = new SqlConnection
("Data Source=10.16.95.210 ; initial Catalog=AMHS ; User ID=kydbo; Password =P@$$w0rd0;"))
using (SqlCommand cmdAMHS = new SqlCommand())
{
if (connAMHS.State == ConnectionState.Closed)
connAMHS.Open();
cmdAMHS.Connection = connAMHS;
string szsql = String.Format(@"select A.*, B.* from AMHS_Bobbin A, AMHS_CarouselMap B
where A.PGroupID=5 and A.PDeviceTypeID in (15,16,25)
and A.PMainDeviceID = B.MainDeviceID and A.CSubDeviceID = B.SubDeviceID
and A.PCarouselPeg = B.Peg");
cmdAMHS.CommandText = szsql;
DataTable dt = new DataTable();
using (SqlDataReader reader = cmdAMHS.ExecuteReader())
dt.Load(reader);
this.dataGridView3.DataSource = dt;
foreach (DataRow DRow in dt.Rows)
{
object[] RowArray;
RowArray = DRow.ItemArray;
string AMHS25 = RowArray[25].ToString();
if (AMHS25 != "")
{
if (AMHS25 == "1")
RowArray[25] = "601";
if (AMHS25 == "2")
RowArray[25] = "602";
if (AMHS25 == "3")
RowArray[25] = "603";
DRow.ItemArray = RowArray;
this.dataGridView3.Rows.Add(DRow.ItemArray);
有必要把ItemArray特地拿出來處理嗎??
通常直接讀取、修改DataRow的欄位值就好了說....
<pre class="c" name="code">
foreach (DataRow DRow in dt.Rows)
{
string AMHS25 = DRow[25].ToString();
if (AMHS25 != "")
{
if (AMHS25 == "1")
DRow[25] = "601";
if (AMHS25 == "2")
DRow[25] = "602";
if (AMHS25 == "3")
DRow[25] = "603";
}
}
dataGridView3.DataSource = dt;
dataGridView3.DataBind();